How to Concatenate Images Using Pillow in Python?


Python is a popular programming language used by developers worldwide to build various applications. One of the significant advantages of Python is the availability of several powerful libraries that simplify complex tasks. One such library is Pillow, which is used for image−processing tasks like resizing, cropping, and manipulating images.

In this tutorial, we will explore how to use Pillow to concatenate images horizontally and vertically in Python. Image concatenation is the process of combining two or more images into a single image. By concatenating images, we can create stunning image collages, combine multiple images into a single image, or create an image sequence. We will dive into the process of loading images using Pillow, resizing them, and finally concatenating them horizontally and vertically in the subsequent section of this article.

How to Concatenate images using Pillow in Python?

In image processing, concatenation refers to combining two or more images into a single image. Image concatenation is a powerful technique that allows you to create stunning image collages, combine multiple images into a single image, or create an image sequence. In this tutorial, we will learn how to concatenate images using the Pillow library in Python.

Before we start, we need to install the Pillow library. In this tutorial, we assume that you have Python installed on your system.

To install the Pillow library, we can use pip, which is the package installer for Python. Open your terminal or command prompt and enter the following command:

pip install Pillow

This command will download and install the Pillow library along with its dependencies. We can now move on to the next section of the article, where we will learn how to load images using Pillow.

Concatenating Images using Pillow

Now that we have installed Pillow, let's move on to using it for concatenating images.

Concatenation means combining multiple images into a single image. We can concatenate images horizontally or vertically.

In order to concatenate images using Pillow, we use the concatenate() method of the Image module. The concatenate() method takes two parameters: images and direction. The images parameter is a list of images that we want to concatenate, and the direction parameter specifies whether we want to concatenate the images horizontally or vertically.

Horizontal Concatenation

Horizontal concatenation is a process of combining two or more images into a single image horizontally. Here we have used the following two images as "image−1" and "image−2":

Image−1

Image−2

Example

Consider the following code snippet for performing horizontal concatenation using Pillow:

from PIL import Image

# Load the images
image1 = Image.open("image-1.jpg")
image2 = Image.open("image-2.jpg")

# Get the dimensions of the images
width1, height1 = image1.size
width2, height2 = image2.size

# Create a new image with the combined width and the height of the tallest image
new_width = width1 + width2
new_height = max(height1, height2)
new_image = Image.new("RGB", (new_width, new_height))

# Paste the two images onto the new image
new_image.paste(image1, (0, 0))
new_image.paste(image2, (width1, 0))

# Save the new image
new_image.save("concatenated_image_horizontal.jpg")

In this code snippet, we first load the two images that we want to concatenate. We then get the dimensions of the images using the size attribute. We create a new image object with the combined width of both images and the height of the tallest image using the new() method.

We then paste the two images onto the new image, with the first image (image1) being pasted starting from the top−left corner (0, 0), and the second image (image2) being pasted starting from the top−right corner of the first image (width1, 0). Finally, the new concatenated image is saved to disk as "concatenated_image_horizontal.jpg".

Output

The output of the above code snippet will be a new image named concatenated_image_horizontal.jpg, which will be the horizontal concatenation of the two input images.

As you can see from the output, the two images were horizontally pasted together to create the final image, resulting in a horizontal concatenation.

Vertical Concatenation

Horizontal concatenation is a process of combining two or more images into a single image vertically.

To concatenate images vertically, we just need to change the parameters passed to the paste() method. Here is a code snippet that demonstrates how to concatenate images vertically using Pillow:

Example

from PIL import Image

# Load the images
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")

# Get the dimensions of the images
width1, height1 = image1.size
width2, height2 = image2.size

# Create a new image with the combined width and the height of the tallest image
new_width = max(width1, width2)
new_height = height1 + height2
new_image = Image.new("RGB", (new_width, new_height))

# Paste the two images onto the new image
new_image.paste(image1, (0, 0))
new_image.paste(image2, (0, height1))

# Save the new image
new_image.save("concatenated_image_vertical.jpg")

In the above code snippet, we create a new image object with the combined height of both images and the width of the widest image. The two images are then pasted onto the new image using the paste() method. image1 is pasted at position (0, 0) which is the top−left corner of the new image. image2 is pasted below image1 at position (0, height1), where height1 is the height of image1.

Finally, the concatenated image is saved as "concatenated_image_vertical.jpg" using the save() method.

Output

The output of the above code snippet will be a new image named "concatenated_image_vertical.jpg"

As you can see from the output, the two images were horizontally pasted together to create the final image, resulting in a horizontal concatenation.

Conclusion

In this tutorial, we learned how to concatenate images using Pillow in Python. We used the Pillow library to load and manipulate the images and then used the concatenate() method of the Image module to concatenate the images horizontally and vertically.

We provided an example for each of the methods, and you can use these examples to create stunning image collages, combine multiple images into a single image, or create an image sequence. By following the steps outlined in this tutorial, you can easily concatenate images in Python and use them in your projects.

Updated on: 21-Jul-2023

490 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements