PIL中的透明PNG事实证明不是透明的 [英] Transparent PNG in PIL turns out not to be transparent
问题描述
我已经用这种方法撞墙了一段时间,所以也许有人可以帮忙.
I have been hitting my head against the wall for a while with this, so maybe someone out there can help.
我正在使用PIL打开具有透明背景和一些随机黑色涂鸦的PNG,然后尝试将其放置在另一个PNG(不具有透明性)的顶部,然后将其保存到第三个文件中.
I'm using PIL to open a PNG with transparent background and some random black scribbles, and trying to put it on top of another PNG (with no transparency), then save it to a third file.
最后它全是黑色,这很令人讨厌,因为我没有告诉它是黑色的.
It comes out all black at the end, which is irritating, because I didn't tell it to be black.
我已经用其他帖子中的多个建议的修复程序对此进行了测试.图像以RGBA格式打开,但仍然混乱.
I've tested this with multiple proposed fixes from other posts. The image opens in RGBA format, and it's still messed up.
此外,该程序应该处理各种文件格式,这就是为什么我使用PIL的原因.具有讽刺意味的是,我尝试的第一种格式是很麻烦的.
Also, this program is supposed to deal with all sorts of file formats, which is why I'm using PIL. Ironic that the first format I tried is all screwy.
任何帮助将不胜感激.这是代码:
Any help would be appreciated. Here's the code:
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)
推荐答案
我认为您要使用的是粘贴蒙版参数.
请参见文档,(向下滚动至paste
)
I think what you want to use is the paste mask argument.
see the docs, (scroll down to paste
)
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)
这篇关于PIL中的透明PNG事实证明不是透明的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!