import numpy as np
import cv2
from PIL import Image
# 修改纯白背景图为透明背景图
def white2transparent(img):
height, width, channel = img.shape
for 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 img
if __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)
png图片背景转换成透明
最新推荐文章于 2024-08-14 23:30:40 发布