In this blog post, I will be showing how to combine images using Python.
I used this code in a for loop to combine hundreds of different images for one of our clients.
These are the two images that we will combine.
Pillow is a PIL fork, install Pillow using pip and import PIL into the Python script.
Next, set the two variables.
These images are saved in the same folder as the script, therefore only the filenames are required to reference them.
To combine them together we use the PIL function paste.
Cat is our starting background image.
1st parameter ‘mouse’ is the image we would like to paste over the top.
2nd parameter ‘(140, 625)’ is coordinates on the background image as to where we would like the image to be pasted.
3rd parameter ‘mouse’ is an optional masking image, as we would like our transparent background mouse to stay transparent, we need to use this.
Unfortunately, the mouse is too big and strays off the background image. Therefore, let’s resize the mouse.
Re-running the code produces.
That’s much better, this is the code in full.
Please find the script and the two images attached.