如何使用PIL将透明png图像与另一个图像合并
问题描述:
我有一个透明的png图片foo.png
我已经打开了另一张图片
I have a transparent png image "foo.png" and I've opened another image with
im = Image.open("foo2.png");
现在我需要的是将foo.png与foo2.png合并。
now what i need is to merge foo.png with foo2.png.
(foo.png包含一些文字,我想在foo2.png上打印该文字)
( foo.png contains some text and I want to print that text on foo2.png )
答
import Image
background = Image.open("test1.png")
foreground = Image.open("test2.png")
background.paste(foreground, (0, 0), foreground)
background.show()
.paste()
的第一个参数是要粘贴的图像。第二个是坐标,秘密酱是第三个参数。它表示将用于粘贴图像的蒙版。如果您传递具有透明度的图像,则Alpha通道将用作遮罩。
First parameter to .paste()
is the image to paste. Second are coordinates, and the secret sauce is the third parameter. It indicates a mask that will be used to paste the image. If you pass a image with transparency, then the alpha channel is used as mask.
检查 docs 。