A
A
Alexander Sankov2021-09-27 00:01:43
Python
Alexander Sankov, 2021-09-27 00:01:43

How to insert an image into an image while getting a meme?

We need a code where supposedly two pictures are glued together - the first under the second.
If the first picture is smaller or larger than the second, then it shrinks or expands.

Here's the resulting code:

from PIL import Image
 
im1 = Image.open('morgen asterix1.jpg') 
im2 = Image.open('morgen asterix2.png')
 
im1.paste(im2)
im1.save('morgen asterix.jpg')
 
im1.close()
im2.close()


spoiler
6150df0e75e6e804187272.png
6150df14c520a914301300.png
6150df204673b427338302.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-27
@astalingrad2015

What is there to think about.
1. Define the list of images you want to glue and upload them.
2. Resize them all to the same width by calling Image.resize() . How you will determine the target width is up to you. You can navigate by the size of the largest of the images.
3. Sum the heights (for scaled versions, not for original ones!), create a new image with the same width and total height (so that all frames fit).
4. Use the Image.paste() methodto insert a frame into the target image. It allows you to specify the position of the image as the second parameter - exactly where the upper left corner of the inserted image will go. So you start at a point (x=0,y=0), and after each insertion you add to y the height of the newly inserted image (plus a gap if you like).
5. Save the final image by calling Image.save() .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question