import numpy as npimport cv2from PIL import Image# 修改纯白背景图为透明背景图def white2transparent(img):height, width, channel = img.shapefor h in range(height):for w in range(width):color = img[h, w]if (color == np.array([255, 255, 255, 255])).all():img[h, w] = [0, 0, 0, 0]return imgif __name__ == '__main__':# 二:白背景图转透明背景图# white_img = cv2.imread('D:/.Download/logo.png')# # print(white_img.shape) # (796, 796, 3)# white_img = cv2.cvtColor(white_img, cv2.COLOR_BGR2BGRA) # 转为4通道# # print(white_img.shape) # (796, 796, 4)# new_transparent = white2transparent(white_img)# cv2.imwrite('new_transparent.png', new_transparent)img=cv2.imread('new_transparent.png',-1)height, width = img.shape[:2]cv2.imshow('img', img)cv2.imwrite('1.jpg', img)# 缩小图像size = (int(width * 0.0125), int(height * 0.0125))shrink = cv2.resize(img, size, interpolation=cv2.INTER_AREA)height1, width1 = shrink.shape[:2]print(height1, width1)cv2.imshow('img',shrink)cv2.imwrite('1.jpg', shrink)cv2.waitKey(0)
1500字范文,内容丰富有趣,写作好帮手!