如何正确组合叠加图像?
How to combine overlay images properly?
我有三张图片。首先,我想并排添加其中两个 home.png
(150 x 150) 和 away.png
(150 x 150)。这些图像是透明的,我也希望输出文件透明,因为我将添加另一个图像作为覆盖。然后,我想在 red.png
(804 x 208) 背景图像的中心添加输出 (300 x 150)。另外,如果可能的话,我想创建输出文件 (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
很遗憾,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
----------------------------------------
我有三张图片。首先,我想并排添加其中两个 home.png
(150 x 150) 和 away.png
(150 x 150)。这些图像是透明的,我也希望输出文件透明,因为我将添加另一个图像作为覆盖。然后,我想在 red.png
(804 x 208) 背景图像的中心添加输出 (300 x 150)。另外,如果可能的话,我想创建输出文件 (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
很遗憾,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
----------------------------------------