Merge Images Horizontally Using Python Pillow

In this tutorial, we will use an example to show how to merge images horizontall using python pillow library. We will merge three images.

1. Import library

from PIL import Image

2. Read images

images = [Image.open(x) for x in ['img1.jpg', 'img2.jpg', 'img3.jpg']]

3. Get total width and maximum height

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])

4. Create an image to paste images with total width and maximum height

new_img = Image.new('RGB', (total_width, max_height))

5. Add all images to new image

current_width = 0
for img in images:
  new_img.paste(img, (current_width,0))
  current_width += img.size[0]

6. Save image

new_img.save('NewImage.jpg')