【OpenCV】将图片黑色背景变成透明背景

tmp = cv2.cvtColor(crop_image, cv2.COLOR_BGR2GRAY)
_, alpha = cv2.threshold(tmp, 0, 255, cv2.THRESH_BINARY)
b, g, r = cv2.split(crop_image)
rgba = [b, g, r, alpha]
dst = cv2.merge(rgba, 4)
# 注意保存成png格式!!!jpg的话还是黑色背景(255)
cv2.imwrite("test.png", dst)
 

你可能感兴趣的:(OpenCV相关,python,opencv)