1

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:

enter image description here enter image description here

The background image:

enter image description here

The expected output:

enter image description here

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
HansHirse
  • 18,010
  • 10
  • 38
  • 67
niko
  • 49
  • 5
  • Welcome to Stack Overflow. Please consider [taking the tour](https://stackoverflow.com/tour), and have a look at [how to ask](https://stackoverflow.com/help/how-to-ask) (good) questions around here. Please provide a [mre] to your problem, i.e. the full code needed to reproduce your actual problem, and provide all needed input images next to the desired output, e.g. manually drawn. – HansHirse Feb 02 '21 at 12:47
  • Thanks i added images and desired output – niko Feb 02 '21 at 12:55

1 Answers1

2

Unfortunately, the overlay image wasn't given. But, I think, for the expected output, it's not necessary at all. I basically cleaned up your code. By that, the erroneous Image.blend call was removed, too. (If you also want to fix that issue, kindly provide the overlay image.) The only things left to do were determining the proper coordinates for the match image inside the background image and paying attention to the transparencies.

The intermediate match.png doesn't need to be saved either, see the following code:

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')

That'd be the output:

Output

To maintain transparency of the match image when pasting to the background image, do not forget to properly set the mask parameter, cf. this Q&A.

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.8.5
Pillow:        8.1.0
----------------------------------------
HansHirse
  • 18,010
  • 10
  • 38
  • 67