Put your images in order (to animate them)

If you got images with the same name followed by a progressive number like

img1.png

img2.png

img3.png

you can easily grab them with a for loop

list_img = []
for img in glob.glob("*.png"):
    list_img.append(img)

or with a list comprehension:

lim = [x for x in glob.glob("*.png")]

*you have to import glob, of course.

With this loops you have this:

>>> lim
["img1.png","img2.png","img3.png"]

So, for your purposes (animation for example), you can load all the files without having to load each one specifing their names. If you have a lot of images you can see how you can save a lot of time with this code.

– Where is the problem then? you may ask.

The problem is when you go from img9.png to img10.png. You may thing that img10.png will be positioned after img9.png. You can try yourself and you will find that the img10.png and also img11.png (untile img.19) will be after img1.png. Also img20.png will not be in place, but after img2.png. So, I thought to solve this in a simple manner. Go on reading if you want to see the code that does the work for you.

lim = [x for x in glob.glob("*.png")]
>>> lim
["img1.png","img10.png","img11.png","img2.png","img3.png","img4.png","img5.png","img6.png","img9.png"]

So.. 1, 10, 11, 2, 3… you can see that the progression is gone. Computers cannot count in the right way? No, it’s just that the name of the files are treated like strings and “10” cames after “1”. Even “100” comes after “10” and not after “99”. So, let’s think about how to solve this little annoying problem we have with progressively numbered files.

To put the images in order use this code

import glob

def order(img):
	serie = img
	im = glob.glob(f"{serie}*.PNG")
	lenim = len(im[0])
	images = [img for img in glob.glob(f"{serie}*.png")
	if len(img) == lenim]
	images2 = [img for img in glob.glob(f"{serie}*.png") if len(img) > lenim]
	images.extend(images2)
	return images

ord_imgs = order("")

As you can see we solved this problem with the following step by step path:

  • load all the files that ends with .PNG
  • check the length of the first one (16)
  • make a list out of the full list in which you choose only the names with 16 characters (they have 1,2…9  at the end)
  • make another list that orders all the files that hase more than 16 (16 + 1) characters (from 10 to… 99).
  • join the first list (1…9) to the second one (10…20 or more)

Now the order is right because the 1 figure numbered files are followed by the 2 figure numbered files and all is in the order that us humans would expect from a progressive list of numbers.

If you try, you can also find a way to make this work even for more than 100 imgs or 1000 or any number. You just hat to make a list for each time the numer add a figure (is this the right word in english? I mean when you pass from 10 to 100 to 1000….).

When did I used this code

I used to load the images to animate a sprite in pygame and to make an animated gif with the images made in different slides of powerpoint and saved as png files and then turned into a gif. You can find my post about this scripts on this blog.

The pygame output

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.