Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 如何正确组合叠加图像?_Python_Image Processing_Python Imaging Library - Fatal编程技术网

Python 如何正确组合叠加图像?

Python 如何正确组合叠加图像?,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我有三张照片。首先,我想将其中的两个home.png(150 x 150)和away.png(150 x 150)并排添加。这些图像是透明的,我还希望输出文件透明,因为我将添加另一个图像作为覆盖。然后,我想在背景图像的中心添加输出(300 x 150),背景图像是red.png(804 x 208)。此外,如果可能的话,我希望创建输出文件(300 x 150),而不将其保存在磁盘上。(home徽标和away徽标是URL。) 将并排显示的图像: 背景图像: 预期产出: 到目前为止,我的代码

我有三张照片。首先,我想将其中的两个
home.png
(150 x 150)和
away.png
(150 x 150)并排添加。这些图像是透明的,我还希望输出文件透明,因为我将添加另一个图像作为覆盖。然后,我想在背景图像的中心添加输出(300 x 150),背景图像是
red.png
(804 x 208)。此外,如果可能的话,我希望创建输出文件(300 x 150),而不将其保存在磁盘上。(home徽标和away徽标是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
回溯(最近一次呼叫最后一次):
文件“/home/emin/Documents/Match Site/test.py”,第39行,在
new_img=Image.blend(背景,叠加,0.5)
文件“/usr/lib/python3.9/site packages/PIL/Image.py”,第2987行,混合格式
返回im1.\新(核心混合(im1.im、im2.im、alpha))
ValueError:图像不匹配

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

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

从PIL导入图像
#读取并调整徽标大小
大小=(150150)
image1=Image.open('home.png')。调整大小(大小)
image2=Image.open('away.png')。调整大小(大小)
#树立匹配形象;将两个徽标粘贴到空图像上
new_image=image.new('RGBA',(2*大小[0],大小[1]))
新图片粘贴(图片1,(0,0))
新建_image.paste(image2,(image1.size[0],0))
#新建_image.save('match.png')#不一定需要
#建立全面产出;将匹配图像粘贴到背景图像w.r.t.上
#真坐标
背景=图像。打开('red.png')。转换('RGBA'))
bg_size=background.size
锚点=(int((bg_大小[0]-2*大小[0])/2),int((bg_大小[1]-size[1])/2))
背景.粘贴(新图片、锚定、新图片)
background.save('image.png'))
这就是输出:

要在粘贴到背景图像时保持
匹配
图像的透明度,请不要忘记正确设置
掩码
参数,参见

----------------------------------------
系统信息
----------------------------------------
平台:Windows-10-10.0.16299-SP0
Python:3.8.5
枕头:8.1.0
----------------------------------------

欢迎来到堆栈溢出。请考虑一下,看看这里的(好)问题。请提供问题的答案,即再现实际问题所需的完整代码,并在所需输出旁边提供所有所需的输入图像,例如手动绘制。谢谢,我添加了图像和所需输出