Python 图片白底透明化处理

from PIL import Image

# 将白色及近似白色的地方转成透明
def changepixel(img):
    picture = Image.open(img).convert('RGBA')
    datas = picture.getdata()
    new_data = []
    for item in datas:

        if item[0] > 220 and item[1] > 220 and item[2] > 220:
            new_data.append((255, 255, 255, 0))
        else:
            new_data.append(item)
    picture.putdata(new_data)
    picture.save('new.png')

changepixel(r'C:\Users\Administrator\Desktop\test.png')


请添加图片描述
请添加图片描述

你可能感兴趣的:(python,PIL)