Merge two images in Python

In this article, we will discuss how to merge two images using Python. For this purpose, we will make use of the Pillow Imaging Library (PIL) of Python.

Now we are going to see the step by step process to do our task.

Installation of Pillow:

The Pillow library doesn’t come inbuilt with Python, we have to install it separately. To install it, we can use the Python package manager pip. Run the following command on your command prompt and it will install everything that’s necessary.

pip install Pillow

You can check if the package was correctly installed by running the following in the Python shell.

import PIL

Once you’re done with the installation, we will get directly into the coding segment.

Python program to merge two images

We can merge two images horizontally or vertically and the process is very similar except for one small change.

The first step is to import the Image class of the PIL library. We need to create an Image object for each of the images that we need to merge. We will store these objects in a list for convenience.

The final merged image will have a width that’s equal to the sum of the widths of all the images and a height that’s equal to the maximum of heights of all the images. This will be the other way around if you combine the images vertically.

from PIL import Image

# Open images and store them in a list
images = [Image.open(x) for x in ['path/to/image1', '/path/to/image2']]
total_width = 0
max_height = 0


# find the width and height of the final image
for img in images:
    total_width += img.size[0]
    max_height = max(max_height, img.size[1])

We need to create a new image and then write the contents of the smaller images one by one to the new final image.
The contents of each new image will be copied with an offset along the rows whereas this offset will be along the columns if you are merging them vertically.

# create a new image with the appropriate height and width
new_img = Image.new('RGB', (total_width, max_height))

# Write the contents of the new image
current_width = 0
for img in images:
  new_img.paste(img, (current_width,0))
  current_width += img.size[0]

# Save the image
new_img.save('NewImage.jpg')

After writing the contents of the new image, save the new image with the desired filename.

The complete Python code to merge our two images will look as follows:

from PIL import Image

# Open images and store them in a list
images = [Image.open(x) for x in ['img1.jpg', 'img2.jpg', 'img3.jpg']]
total_width = 0
max_height = 0


# find the width and height of the final image
for img in images:
    total_width += img.size[0]
    max_height = max(max_height, img.size[1])

# create a new image with the appropriate height and width
new_img = Image.new('RGB', (total_width, max_height))

# Write the contents of the new image
current_width = 0
for img in images:
  new_img.paste(img, (current_width,0))
  current_width += img.size[0]

# Save the image
new_img.save('NewImage.jpg')

I hope this article was helpful to you and solved your problem. The Pillow library has several extensive features for handling images.

See also:

Leave a Reply

Your email address will not be published. Required fields are marked *