Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用PIL将一个透明png图像与另一个图像合并_Python_Image_Image Processing_Python Imaging Library - Fatal编程技术网

Python 如何使用PIL将一个透明png图像与另一个图像合并

Python 如何使用PIL将一个透明png图像与另一个图像合并,python,image,image-processing,python-imaging-library,Python,Image,Image Processing,Python Imaging Library,我有一个透明的png图像“foo.png” 我已经打开了另一张图片 im = Image.open("foo2.png"); 现在我需要的是将foo.png与foo2.png合并 (foo.png包含一些文本,我想在foo2.png上打印这些文本) .paste()的第一个参数是要粘贴的图像。第二个是坐标,第三个参数是秘密酱汁。它表示将用于粘贴图像的遮罩。如果传递具有透明度的图像,则alpha通道将用作遮罩 检查。图像。当背景图像也包含透明度时,粘贴将无法按预期工作。你需要用真的 Pillow

我有一个透明的png图像“foo.png” 我已经打开了另一张图片

im = Image.open("foo2.png");
现在我需要的是将foo.png与foo2.png合并

(foo.png包含一些文本,我想在foo2.png上打印这些文本)

.paste()
的第一个参数是要粘贴的图像。第二个是坐标,第三个参数是秘密酱汁。它表示将用于粘贴图像的遮罩。如果传递具有透明度的图像,则alpha通道将用作遮罩


检查。

图像。当背景图像也包含透明度时,粘贴将无法按预期工作。你需要用真的

Pillow 2.0包含一个执行此操作的
alpha_composite
函数

background = Image.open("test1.png")
foreground = Image.open("test2.png")

Image.alpha_composite(background, foreground).save("test3.png")
编辑:两个图像都需要为RGBA类型。因此,如果它们是调色板,则需要调用
convert('RGBA')
,等等。。如果背景没有alpha通道,则可以使用常规粘贴方法(应该更快)

正如olt已经指出的,
Image.paste
在源和目标都包含alpha时无法正常工作

考虑以下场景:

两个测试图像,均包含alpha:

layer1=Image.open(“layer1.png”)
layer2=Image.open(“layer2.png”)
使用
图像合成图像。按如下方式粘贴

final1=Image.new(“RGBA”,layer1.size)
最终1.粘贴(第1层,(0,0),第1层)
最终1.粘贴(第2层,(0,0),第2层)
生成以下图像(叠加红色像素的alpha部分完全取自第二层。像素未正确混合):

使用
image.alpha_composite
合成图像,如下所示:

final2=Image.new(“RGBA”,layer1.size)
final2=Image.alpha_复合(final2,第1层)
final2=图像.alpha_组合(final2,第2层)
生成以下(正确)图像:


也可以使用混合:

im1 = Image.open("im1.png")
im2 = Image.open("im2.png")
blended = Image.blend(im1, im2, alpha=0.5)
blended.save("blended.png")

有一个类似的问题,很难找到答案。以下函数允许您将带有透明度参数的图像粘贴到另一图像上的特定偏移处

import Image

def trans_paste(fg_img,bg_img,alpha=1.0,box=(0,0)):
    fg_img_trans = Image.new("RGBA",fg_img.size)
    fg_img_trans = Image.blend(fg_img_trans,fg_img,alpha)
    bg_img.paste(fg_img_trans,box,fg_img_trans)
    return bg_img

bg_img = Image.open("bg.png")
fg_img = Image.open("fg.png")
p = trans_paste(fg_img,bg_img,.7,(250,100))
p.show()

最后,我把用户@p.Melch和@Mithril对我正在进行的一个项目提出的建议编写成代码

我也对越界安全进行了编码。(我链接了一个特定的提交,因为这个存储库的未来可能会发生变化)

注意:我期望像so
np.array(Image.open(…)
这样的图像中的numpy数组作为
copy\u from
的输入A和B,以及这个链接函数的
overlay
参数

依赖项是它前面的函数、
copy\u from
方法以及作为切片的PIL图像内容的numpy数组

虽然该文件非常面向类,但如果您想使用该函数
overlay\u transparent
,请确保将
self.frame
重命名为背景图像numpy数组

或者您可以复制整个文件(可能会删除一些导入和
Utils
类),然后像这样与该框架类交互:

# Assuming you named the file frame.py in the same directory
from frame import Frame

background = Frame()
overlay = Frame()

background.load_from_path("your path here")
overlay.load_from_path("your path here")

background.overlay_transparent(overlay.frame, x=300, y=200)
然后,将您的
background.frame
作为叠加和alpha合成的数组,您可以使用
overlayed=image.fromarray(background.frame)
或类似的方式从中获取PIL图像:

overlayed = Frame()
overlayed.load_from_array(background.frame)
或者直接从alpha合成的内部
self.frame
变量获取
background.save(“保存路径”)

您可以阅读该文件并找到我编写的这个实现的其他一些不错的函数,如方法
获取rgb帧数组
按比例调整大小
按分辨率调整大小
旋转
高斯模糊
透明度
渐晕

您可能希望删除
resolve\u pending
方法,因为这是该项目特有的方法



如果我帮了你,很高兴,一定要查看我正在谈论的回购协议,这个问题和线程在开发过程中帮了我很多忙:)

不要使用
在python命令的末尾:这太难看了…我会记住的,谢谢!!要确保前景在所有情况下都具有透明度,请对掩码参数使用
foreground.convert('RGBA')
。谢谢。我太缺少第三个参数了。我得到了
ValueError:bad transparency mask
秘方是tasty@DenizOzger要修复
ValueError:bad transparency mask
使用
bg.paste(fg,(0,0),fg.convert('RGBA'))
我刚刚使用paste()将一个半透明图像叠加到另一个半透明图像上,使用PIL,这一切都如我所料。它在什么方面没有达到您的预期效果?@PeterHansen,paste()没有达到预期效果“当背景图像也包含透明度时”。@PeterHansen有一个例子:@homm谢谢。那是很久以前的事了,我不记得我做过什么。看来我确实错过了你提到的关于背景图像也有透明度的部分。我得到了
ValueError:image做错了
以及@DenizOzgerThanks的截图!真的很有帮助!但是
alpha_composite
无法设置偏移量,您是否介意举一个例子来完全替换
paste
功能?我猜您必须创建一个与garget图像大小相同的新空图像,将图层粘贴到适当的位置,并使用alpha_合成将新图像混合到目标图像上。我得到:ValueError:图像不匹配这张图片对我来说非常有效。图像的大小必须完全相同,但没有问题。粘贴函数没有为我完全剪切它…“ValueError:图像不匹配”可能,它们的尺寸不同。您可能需要缩放或裁剪其中一个。@Schütze看到nvd的评论,因为他/她没有ping(使用@blahblah)youHi,您能为您的答案添加一点上下文吗?其他
# Assuming you named the file frame.py in the same directory
from frame import Frame

background = Frame()
overlay = Frame()

background.load_from_path("your path here")
overlay.load_from_path("your path here")

background.overlay_transparent(overlay.frame, x=300, y=200)
overlayed = Frame()
overlayed.load_from_array(background.frame)