Python的函数库OpenCV真的很强大,前不久刚领略OpenCV对验证码图片进行降维来在selenium模拟登录之后破解验证码。今天又尝试用OpenCV对证件照进行换底色。太好用了,直接上源码。
import cv2import numpy as npimg = cv2.imread(r"PicturePath") #读取原图片hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) #照片在hsv状态色彩可查#由蓝底转换为其他颜色的阈值lower_blue = np.array([90,70,70])upper_blue = np.array([110,255,255])mask = cv2.inRange(hsv,lower_blue,upper_blue) #黑白处理#由红底变为其他颜色时候的阈值# lower_red = np.array([0,125,125])# upper_red = np.array([255,255,255])# mask = cv2.inRange(hsv,lower_red,upper_red) #黑白处理cv2.imshow('Mask',mask) #查看cv降维后的黑白图片,即人像轮廓#颜色替换rows,cols,channels = img.shapefor i in range(rows):for j in range(cols):if mask[i,j] == 255:# img[i,j] = (255, 255, 255) #替换为白底img[i,j] = (0, 0, 255) #替换为红底# img[i, j] = (255, 0, 0) # 替换为蓝底cv2.imshow('res',img) #查看处理完成的图片# cv2.imwrite(r"blue_2_white.jpg",img)# cv2.imwrite(r"blue_2_blue.jpg",img)cv2.imwrite(r"blue_2_red.jpg",img)cv2.waitKey(0) #无限等待,防止卡死cv2.destroyAllWindows() #销毁内存