Python PIL – Merge images

In this post we will be creating a Python script that will merge multiple images into a single image, to do this we will be using the Python Imaging Library also known as PIL enabling us to image processing capabilities.

See the sample of Python code below where we utilise the PIL library to merge two images into a single image and save it as a new file.

To achieve this we start by opening both images and resize each of them to an equal width and height. We then create a new blank image and paste our two images in side by side. This is then saved as a new file named ‘image.png’.

from PIL import Image"image1.png")"image2.png")

img1=img1.resize((600, 600))
img2=img2.resize((600, 600))"RGB", (1200, 600))
img.paste(img1, (0, 0))
img.paste(img2, (600, 0))"image.png")

See below our image ‘img1.png’.

first image used for merging in Python

See below our image ‘img2.png’.

second image used for merging in Python

See below our merged image.

Merged image in Python using PIL library

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply