简体   繁体   中英

Making a long image without resizing

I need to put many images together side by side but without changing the height or width of any of them. That is to say, it will just be one image of a constant height but very long width as the image are sitting horizontally.

I've been using Python and the PIL library but what I've tried so far is producing an image that makes all the images smaller to concatenate into one long image.

Image.MAX_IMAGE_PIXELS = 100000000  # For PIL Image error when handling very large images

imgs = [ Image.open(i) for i in list_of_images ]

widths, heights = zip(*(i.size for i in imgs))
total_width = sum(widths)
max_height = max(heights)

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

# Place first image
new_im.paste(imgs[0],(0,0))

# Iteratively append images in list horizontally
hoffset=0
for i in range(1,len(imgs),1):
    hoffset=imgs[i-1].size[0]+hoffset  # update offset**
    new_im.paste(imgs[i],(hoffset,0))

new_im.save('row.jpg')

The result I'm getting now is one image made up of concatenated images in a horizontal row. This is what I want, except the images are being made smaller and smaller in the concatenation process. I want the end result to not make the images smaller and instead produce an image made of the input images with their original size. So the output image will just have to have a very long width.

It seems you have a bug while updating the offsets. You should replace your iteration block with:

imgs = [Image.open(i) for i in list_of_images]
widths, heights = zip(*(i.size for i in imgs))

new_img = Image.new('RGB', (sum(widths), max(heights)))
h_offset = 0
for i, img in enumerate(imgs):
    new_img.paste(img, (h_offset, 0))
    h_offset += img.size[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM