Home >Backend Development >Python Tutorial >How to Combine Images Horizontally in Python Without Partial Segments?
Combining Images Horizontally in Python
Aiming to horizontally combine multiple JPEG images, you may encounter challenges like additional partial images appearing in the output. Here's a solution that addresses this issue.
Problem:
You have three 148 x 95 pixel images, and you want to combine them horizontally without any unwanted extra segments.
Solution:
Calculate Dimensions:
Create a New Image:
Paste Images:
Using this modified code:
<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>
will produce the desired horizontally combined image without any partial segments.
The above is the detailed content of How to Combine Images Horizontally in Python Without Partial Segments?. For more information, please follow other related articles on the PHP Chinese website!