简体   繁体   English

如何在python枕头中合并2个透明png

[英]How to merge 2 transparent png's in python Pillow

I have 2 transparent PNG images of the same size (142,43). 我有2张相同大小的透明PNG图片(142,43)。 I am trying to vertically stack them. 我正在尝试垂直堆叠它们。 This is one of them: 这是其中之一:

是

The end result should be like this (142,86): 最终结果应如下所示(142,86):

是

It also should retain its transparancy. 它还应保持其透明度。

I've tried the following code: 我尝试了以下代码:

from PIL import Image

img_list = [Image.open("example.png"), Image.open("example.png")]
bg = Image.open("1x1_transparent.png")

bg = bg.resize(size=(142, 43*2))
img_list[0] = img_list[0].convert('RGBA')

bg.paste(img_list[0], (0, 0), img_list[0])

bg.save('final.png')

Which imports a 1x1 transparent image, resizes it to the final target size, then tries to put the first image on it. 它将导入一个1x1透明图像,将其调整为最终的目标尺寸,然后尝试在其上放置第一个图像。 This does not work. 这是行不通的。 The saved image 'final.png' shows an empty image. 保存的图像“ final.png”显示为空图像。

Any thoughts what I would be doing wrong? 有什么想法我会做错什么吗?

If your output doesn't seem properly sized, it's probably because of this line: 如果您的输出大小似乎不正确,则可能是由于以下原因:

bg.resize(size=(142, 43*2))

resize returns a new version of the image, leaving the original one unmodified. resize返回该图像的新版本,而原始版本则保持不变。 Try assigning the returned value to something so you can do additional operations on it and ultimately save the output. 尝试将返回值分配给某些对象,以便您可以对其进行其他操作并最终保存输出。

bg = bg.resize(size=(142, 43*2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM