相关疑难解决方法(0)

使用PIL使所有白色像素透明?

我正在尝试使用Python Image Library使所有白色像素透明.(我是一个C黑客试图学习python所以要温柔)我已经有转换工作(至少像素值看起来正确)但我无法弄清楚如何将列表转换为缓冲区来重新创建图片.这是代码

img = Image.open('img.png')
imga = img.convert("RGBA")
datas = imga.getdata()

newData = list()
for item in datas:
    if item[0] == 255 and item[1] == 255 and item[2] == 255:
        newData.append([255, 255, 255, 0])
    else:
        newData.append(item)

imgb = Image.frombuffer("RGBA", imga.size, newData, "raw", "RGBA", 0, 1)
imgb.save("img2.png", "PNG")
Run Code Online (Sandbox Code Playgroud)

python image python-imaging-library

54
推荐指数
6
解决办法
7万
查看次数

标签 统计

image ×1

python ×1

python-imaging-library ×1