将透明背景转换为白色
按尺寸剪切图片截取图片
import cv2,os
def transparence2white(img):
sp = img.shape
width = sp[0]
height = sp[1]
for yh in range(height):
for xw in range(width):
color_d = img[xw, yh]
if (color_d[3] == 0):
img[xw, yh] = [255, 255, 255, 255]
return img
if __name__ == '__main__':
imgPath = "%s/bin/" % os.getcwd()
newfile = imgPath + 'new.jpg'
image_path = imgPath + '1.jpg'
print(imgPath, newfile, image_path)
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
img = transparence2white(img)
x, y, h, w= 0,130,900,1059
cropImg = img[(y):(y + h), (x):(x + w)]
cv2.imwrite(newfile, cropImg)