本文介绍了如何使用 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 )
推荐答案
from PIL 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.
查看文档.
这篇关于如何使用 PIL 将透明 png 图像与另一个图像合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!