user4914499
user4914499

Reputation: 354

How to sort input PNG files in ImageMagick

I am making an animated GIF file using following command:

convert -delay 10 -loop 0 *.png animated.gif

But, the problem happens in the order of *.png.

My PNG files are like 1.png, 2.png, ... 100.png.

In the resulting animated GIF, the order of frames are like: 1.png, 10. png, 100.png, 2.png ...

I want that in the resultant GIF the snapshot are in order like 1,2,3 ... 100.

Upvotes: 5

Views: 2454

Answers (4)

Mark Setchell
Mark Setchell

Reputation: 207425

This little script will rename your files for you with leading zeroes - try it on a COPY of your files first:

#!/bin/bash
for i in {1..1000}; do
   [ -f "$i.jpg" ] && echo mv "$i.jpg" $(printf "%04d.jpg" $i)
done

It looks for JPEG files from 1.jpg through 1000.jpg and if any exist, it renames then with up to 3 leading zeroes till the number part is 4 digits wide.

If you save it as renamer, you will then run

chmod +x renamer    # just do this once to make it executable
./renamer           # do this any time you want the whole directory of JPEGs renamed

Remove the word echo if you see and like what it is going to do, then run it again.

Sample output

mv 1.jpg 0001.jpg
mv 99.jpg 0099.jpg
mv 102.jpg 0102.jpg

Upvotes: 1

Peter Paul Kiefer
Peter Paul Kiefer

Reputation: 2124

The glob operator (*) sorts the result list by comparing texts. So renaming the files 1.png, 2.png ... to 001.png, 002.png should work.

Upvotes: 0

Kiran Dash
Kiran Dash

Reputation: 4956

Rename your images with leading zeroes like:

001.png, 002.png, 003.png,......099.png,100.png.

And it will work fine for you

Upvotes: 0

NarūnasK
NarūnasK

Reputation: 4950

Please try sort:

$ ls | sort -V
1.png
2.png
10.png
100.png

So ultimately:

convert -delay 10 -loop 0 $(ls *.png | sort -V) animated.gif

Upvotes: 9

Related Questions