一行代码 python 证件照换底色

安装所需模块

pip install OpenCV-python
 
pip install cvzone
 
pip install mediapipe

代码

import cv2
import cvzone
from cvzone.SelfiSegmentationModule import SelfiSegmentation

# 原图
img = cv2.imread('01.jpg', cv2.IMREAD_UNCHANGED)

# 背景图(纯白色) 需要和原图大小一致
img_b = cv2.imread('b1.jpg', cv2.IMREAD_UNCHANGED)

segmentor = SelfiSegmentation()

# 核心的一行代码
imgOut = segmentor.removeBG(img, img_b, threshold=0.3)

cv2.imshow("01.jpg", img)
cv2.imshow("imgOut", imgOut)

cv2.waitKey()
cv2.destroyAllWindows()

效果

posted @ 2022-02-18 08:51  天天代码码天天  阅读(42)  评论(0)    收藏  举报  来源