Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select images with numbered filenames and append all images together horizontally

I have several image files whose names are numbered 1-300. (frame1jpg, .. ,frame300.jpg)

I would like to append all images together horizontally by select (n) and Start/End file.

And I can change (n) and start/end files.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example1: (n) = 5, start frame5.jpg end frame30.jpg. After that append from frame5.jpg, frame10.jpg,..,frame25.jpg

Example2: (n) = 7, start frame0.jpg end frame25.jpg. After that append from frame0.jpg, frame7.jpg,..,frame21.jpg

Now I have code to append image as below.

import sys
from PIL import Image

images = [Image.open(x) for x in ['frame45.jpg','frame55.jpg','frame65.jpg','frame75.jpg','frame85.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('00final.png')

I’d like to change from.

images = [Image.open(x) for x in ['frame45.jpg','frame55.jpg','frame65.jpg','frame75.jpg','frame85.jpg']]

To easy define (n) and Start/End file.

>Solution :

Just get the list of filenames given your start, end and step:

def get_images(start, end, step):
    file_list = [f"frame{i}.jpg" for i in range(start, end, step)]   
    return [Image.open(x) for x in file_list]

images = get_images(start, end, step)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading