我有一堆图片,我需要把文字覆盖在上面。我用GIMP (具有透明度的PNG)创建了覆盖,并尝试将其粘贴到其他图像之上:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")然而,与其在顶部显示一个漂亮的黑色文本,我还得到了以下内容:

在Gimp中,overlay.png看起来是这样的:

所以我会期待一些漂亮和黑色的文字,而不是这五颜六色的混乱。
有什么想法吗?我错过了一些PIL选项?
发布于 2016-01-03 15:03:04
正如vrs在上面指出的那样,使用类似于以下答案的alpha_composite:How to merge a transparent png image with another image using PIL
就能做到这一点。确保图像处于正确的模式(RGBA)。
完整解决方案:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")结果:

https://stackoverflow.com/questions/34577598
复制相似问题