PIL中的透明PNG结果不透明

Mar*_*eur 23 python png transparent python-imaging-library

我一直在墙上撞了一会儿,所以也许有人可以提供帮助.

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 38

我想你想要使用的是paste mask参数.看文档,(向下滚动到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)
Run Code Online (Sandbox Code Playgroud)

  • 好样的!PIL文档并不清楚掩码是什么 - 首先我认为它是一个数字(因为他们说'如果掩码是0,如果掩码是255',那么我认为它就像盒子一样)参数,但用于确定将复制图像参数的哪一部分.现在它更有意义,谢谢! (4认同)