How to merge images with same size using the Python 3 module pillow?


The pillow or PIL is a powerful Python library for handling and manipulating images. One of the most common tasks in image processing is merging multiple images into a single image. It provides a range of functions for loading, manipulating, and saving images in various formats. One of the main features of Pillow is its support for image merging. It supports a variety of images including “jpeg”, “png”, “bmp”, “gif”, “ppm”, “tiff”. With this module, we can do almost anything with digital images like basic image processing functionality, point operations, filtering images using built-in convolution kernels, and color space conversions.

In this article, we will learn how to merge images of the same size using the Pillow module in Python 3.

But before we begin, let's first understand what merging images is and why it is important. The merging of images is a process of combining two or more images into a single image. It is commonly used in applications while creating panoramas, compositing multiple images into a single frame, or blending images together to create a new image. It is important to note that in order to merge images, they need to be of the same size.

Steps to Merge Images With Same Size Using Pillow

Here are the steps to merge images with the same size using Pillow in Python3 −

Step 1: Import the Required Modules

To start, we need to import the required modules. In this case, we need to import the Image module from the Pillow library.

from PIL import Image

Step 2: Open the Images

Next, we need to open the images that we want to merge. We can do this using the Image.open() method. This method returns an Image object that we can use to manipulate the image.

myimage1= Image.open('image1.jpg')
myimage1= Image.open('image2.jpg')

Step 3: Check the Sizes of the Images

We need to make sure that the images have the same size before we can merge them. We can check the size of the images using the size attribute.

if myimage1.size == myimage2.size:
   # continue with merging
else:
   print('The images must have the same size to merge them using Pillow!')

Step 4: Create a new Image

We need to create a new image that will hold the merged image. We can do this using the Image.new() method. This method takes the mode and size of the new image as parameters.

mymerged_image= Image.new('RGB', myimage1.size)

Step 5: Merge the Images

Finally, we can merge the images by pasting one image onto the other using the Image.paste() method. We need to specify the coordinates where we want to paste the image.

mymerged_image.paste(myimage1, (0, 0))
mymerged_image.paste(myimage2, (myimage1.size[0], 0))

Step 6: Save the Merged Image

We can save the merged image using the Image.save() method.

mymerged_image.save(mymerged_image.jpg')

Now, you must have a good understanding of how to merge images of the same sizes using the python3 module pillow. Let’s see some examples in detail using different approaches.

Example 1: Horizontally merging of Images

In the below, we will merge two images horizontally. The first image will be on the left, and the second image will be on the right.

We create a new merged image with a width equal to the width of one of the input images and a height equal to the sum of the heights of the two input images. Here, we read the two input images, myimage1, and myimage2, using the Image.open() method.

Next, we check whether the sizes of the two images are the same. If they are, we create a new image using the Image.new() method. The Image.new() method takes two arguments: the mode of the new image (in this case, 'RGB'), and the size of the new image (in this case, the width of the new image is the sum of the widths of the two input images, and the height of the new image is the height of one of the input images).

After creating the new image, we use the paste() method to merge the two input images horizontally. The paste() method takes two arguments: the image to paste and the location to paste it at. In this case, we paste image1 at the top-left corner of the new image ((0, 0)), and myimage2 at the top-right corner of the new image ((myimage1.size[0], 0)). And finally, we saved the merged image.

#import the PIL
from PIL import Image
# Open the images
myimage1 = Image.open('myimageone.jpg')
myimage2 = Image.open('myimagetwo.jpg')

# Checking the sizes of the images
if myimage1.size == myimage2.size:
   # Creatin a new image
   mymerged_image = Image.new('RGB', (myimage1.size[0], myimage1.size[1] + myimage1.size[1]))
   # Merging the images horizontally
   mymerged_image.paste(myimage1, (0, 0))
   mymerged_image.paste(myimage2, (myimage1.size[0], 0))
   # Saving the merged image
   mymerged_image.save('mymerged_image_horizontal.jpg')
else:
   print('The images must have the same size to merge them using Pillow')

Output

Input Images

Output Image

Example 2: Vertically Merging of Images

In the below example, we will merge two images vertically. The first image will be at the top, and the second image will be at the bottom.

We create a new image with a width equal to the sum of the widths of the two input images and a height equal to the height of one of the input images. Now, using the PIL module, we have then pasted the first image at the top-left corner of the new image and the second image at the top-right corner of the new image. And finally, we saved the merged image.

#import the PIL
from PIL import Image

# Open the images
myimage1 = Image.open('myimageone.jpg')
myimage2 = Image.open('myimagetwo.jpg')

# Checking the sizes of the images
if myimage1.size == myimage2.size:
   # Creatin a new image
   mymerged_image = Image.new('RGB', (myimage1.size[0], myimage1.size[1] + myimage1.size[1]))

   # Merging the images vertically
   mymerged_image.paste(myimage1, (0, 0))
   mymerged_image.paste(myimage2, (0, myimage1.size[1]))

   # Saving the merged image
   mymerged_image.save('mymerged_image_vertical.jpg')
else:
   print('The images must have the same size to merge them using Pillow')

Output

Input Images

Output Image

Conclusion

Merging images with the same size is a common task in image processing in Python. In this article, we learned how to merge two images of the same size using the Python 3 module pillow. We saw the complete steps involved in merging images using Pillow in Python and two complete examples of merging images horizontally and vertically. By following these steps, you can easily merge images of the same size using Pillow.

Updated on: 31-Jul-2023

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements