I have three images. First, I want to add side to side two of them home.png
(150 x 150) and away.png
(150 x 150). These images are transparent, and I also want transparency on the output file, because I will add another image as overlay. Then, I want to add the output (300 x 150) on a center of a background image which is red.png
(804 x 208). Also, if possible, I want to create the output file (300 x 150) without saving it on my disk. (The home logo and away logo are URLs.)
Images that will be side by side:
The background image:
The expected output:
My code so far:
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")
The error I get:
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