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 img1=Image.open("image1.png") img2=Image.open("image2.png") img1=img1.resize((600, 600)) img2=img2.resize((600, 600)) img=Image.new("RGB", (1200, 600)) img.paste(img1, (0, 0)) img.paste(img2, (600, 0)) img.save("image.png")
See below our image ‘img1.png’.
See below our image ‘img2.png’.
See below our merged image.
Take a look at some of our other content around the Python programming language by clicking here.