gpt4 book ai didi

python - PIL中的透明PNG结果不是透明的

转载 作者:IT老高 更新时间:2023-10-28 20:32:43 26 4
gpt4 key购买 nike

我的头撞墙已经有一段时间了,所以也许有人可以帮忙。

我正在使用 PIL 打开一个具有透明背景和一些随机黑色涂鸦的 PNG,并尝试将其放在另一个 PNG(不透明)之上,然后将其保存到第三个文件中。

最后出来全黑,很烦人,因为我没说是黑的。

我已经使用其他帖子中的多个建议修复对此进行了测试。图片以 RGBA 格式打开,但还是乱七八糟。

另外,这个程序应该可以处理各种文件格式,这就是我使用 PIL 的原因。具有讽刺意味的是,我尝试的第一种格式很糟糕。

任何帮助将不胜感激。代码如下:

from PIL import Image
img = Image.open(basefile)
layer = Image.open(layerfile) # this file is the transparent one
print layer.mode # RGBA
img.paste(layer, (xoff, yoff)) # xoff and yoff are 0 in my tests
img.save(outfile)

最佳答案

我认为您要使用的是粘贴掩码参数。见docs ,(向下滚动到粘贴)

from PIL import Image
img = Image.open(basefile)
layer = Image.open(layerfile) # this file is the transparent one
print layer.mode # RGBA
img.paste(layer, (xoff, yoff), mask=layer)
# the transparancy layer will be used as the mask
img.save(outfile)

关于python - PIL中的透明PNG结果不是透明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7510313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com