【问题标题】:How to combine overlay images properly?如何正确组合叠加图像?
【发布时间】:2021-02-02 21:23:28
【问题描述】:

我有三张图片。首先,我想将其中两个 home.png (150 x 150) 和 away.png (150 x 150) 并排添加。这些图像是透明的,我还希望输出文件具有透明度,因为我将添加另一个图像作为叠加层。然后,我想将输出 (300 x 150) 添加到背景图像的中心,即red.png (804 x 208)。另外,如果可能的话,我想创建输出文件 (300 x 150) 而不将其保存在我的磁盘上。 (主场标志和客场标志是 URL。)

并排的图像:

背景图片:

预期输出:

到目前为止我的代码:

with open("Files/home.png", 'wb') as f:
        f.write(home_logo.content) 

with open("Files/away.png", 'wb') as f:
        f.write(away_logo.content) 


image1 = Image.open('Files/home.png')
image1.show()
image2 = Image.open('Files/away.png')
image2.show()
#resize, first image
image1 = image1.resize((150, 150))
image1_size = image1.size
image2_size = image2.size
new_image = Image.new('RGB',(2*image1_size[0], image1_size[1]))
new_image.paste(image1,(0,0))
new_image.paste(image2,(image1_size[0],0))
new_image.save("Files/match.png","PNG")
try:
    from PIL import Image
except ImportError:
    import Image

background = Image.open("Files/red.png")
overlay = Image.open("Files/match.png")

background = background.convert("RGBA")
overlay = overlay.convert("RGBA")

new_img = Image.blend(background, overlay, 0.5)
new_img.save("image.png","PNG")

我得到的错误:

Traceback (most recent call last):
  File "/home/emin/Documents/Match Site/test.py", line 39, in <module>
    new_img = Image.blend(background, overlay, 0.5)
  File "/usr/lib/python3.9/site-packages/PIL/Image.py", line 2987, in blend
    return im1._new(core.blend(im1.im, im2.im, alpha))
ValueError: images do not match

【问题讨论】:

  • 欢迎来到 Stack Overflow。请考虑taking the tour,并在此处查看how to ask(好)问题。请为您的问题提供minimal reproducible example,即重现您的实际问题所需的完整代码,并在所需输出旁边提供所有需要的输入图像,例如手工绘制。
  • 感谢我添加了图像和所需的输出

标签: python image-processing python-imaging-library


【解决方案1】:

很遗憾,没有给出overlay 图片。但是,我认为,对于预期的输出,根本没有必要。我基本上清理了你的代码。这样,错误的Image.blend 调用也被删除了。 (如果您也想解决这个问题,请提供overlay 图像。)剩下要做的就是确定背景图像中match 图像的正确坐标并注意透明度。

中间的match.png也不需要保存,见以下代码:

from PIL import Image

# Read and resize logos
size = (150, 150)
image1 = Image.open('home.png').resize(size)
image2 = Image.open('away.png').resize(size)

# Build match image; paste both logos on an empty image
new_image = Image.new('RGBA', (2 * size[0], size[1]))
new_image.paste(image1, (0, 0))
new_image.paste(image2, (image1.size[0], 0))
# new_image.save('match.png')                   # Not necessarily needed

# Build full output; paste match image on background image w.r.t. the
# proper coordinates
background = Image.open('red.png').convert('RGBA')
bg_size = background.size
anchor = (int((bg_size[0] - 2 * size[0]) / 2), int((bg_size[1] - size[1]) / 2))
background.paste(new_image, anchor, new_image)
background.save('image.png')

这就是输出:

要在粘贴到背景图像时保持match 图像的透明度,请不要忘记正确设置mask 参数,参见。 this Q&A.

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.8.5
Pillow:        8.1.0
----------------------------------------

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 2015-04-25
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多