How to Concatenate Images Horizontally in Python: A Quick Guide

Barbara Streisand
Release: 2024-10-26 09:41:02
Original
314 people have browsed it

How to Concatenate Images Horizontally in Python: A Quick Guide

Concatenating Images Horizontally with Python

This article aims to address the challenge of horizontally combining multiple JPEG images using Python.

Problem Statement

Consider a scenario where three images, each with dimensions 148 x 95, need to be joined horizontally to form a single image. A previous attempt using the following code encountered issues:

<code class="python">import sys
from PIL import Image

list_im = ['Test1.jpg','Test2.jpg','Test3.jpg']

new_im = Image.new('RGB', (444,95))

for elem in list_im:
    for i in xrange(0,444,95):
        im=Image.open(elem)
        new_im.paste(im, (i,0))
new_im.save('test.jpg')</code>
Copy after login

Proposed Solution

To address the problem, an improved code is provided below:

<code class="python">import sys
from PIL import Image

images = [Image.open(x) for x in ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']]
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

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

x_offset = 0
for im in images:
  new_im.paste(im, (x_offset,0))
  x_offset += im.size[0]

new_im.save('test.jpg')</code>
Copy after login

The solution includes the following key amendments:

  • The use of zip(*(i.size for i in images)) to determine the overall width and height of the combined image
  • Replacement of the problematic nested loop with a simplified one that iterates through each image in the list
  • Adjustment of the pasting position based on the width of each image to avoid accidental overlapping.

The provisions to avoid hard-coding image dimensions and specify dimensions in a single line (both mentioned in the additional information) are inherent to the improved code.

Explanation

The updated code first calculates the widths and heights of all input images and uses these values to determine the total width and maximum height of the output image.

Next, it creates a blank image with the calculated dimensions.

Then, it iterates through each input image, pasting each on the blank image at the appropriate x-offset (starting from 0), and incrementing the x-offset after each paste to make way for the next image.

Finally, the combined image is saved to a specified filename.

The above is the detailed content of How to Concatenate Images Horizontally in Python: A Quick Guide. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template