我已经有一阵子碰到这个问题了,所以也许外面有人能帮上忙。
我正在使用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)
发布于 2011-09-22 17:25:32
我认为您想要使用的是paste mask参数。查看docs (向下滚动到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)
https://stackoverflow.com/questions/7510313
复制相似问题