Defining the file order for ImageMagick convert

2020-06-03 05:25发布

问题:

I have a bunch of PNG files named foo<bar>.png I wish to convert to TIF animation. <bar> is a number varies from 0 to 25 in leaps of five. ImageMagick place foo5.png last in the animation while it is supposed to be second. Is there a way, apart from renaming the file to foo05.png to place it in the right place?

回答1:

You just give the order of your PNG files as they should appear in the animation. Use:

foo0.png foo5.png foo10.png foo15.png foo20.png foo25.png

instead of

foo*.png

After all, it's only 6 different file names which should be easy enough to type:

convert                                                      \
  -delay 10                                                  \
   foo0.png foo5.png foo10.png foo15.png foo20.png foo25.png \
  -loop 0                                                    \
   animated.gif


回答2:

If you have more input images than are convenient enough to type (say, foo0..foo100.png), you could do this (on Linux, Unix and Mac OS X):

convert                                                  \
  -delay 10                                              \
   $(for i in $(seq 0 5 100); do echo foo${i}.png; done) \
  -loop 0                                                \
   animated.gif


回答3:

Simple and easy, list your images and sort them:

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


回答4:

You can use "find" with "sort":

convert -delay 10 $(find . -name "*.png" -print0 | sort -zV | xargs -r0 echo) -loop 0 animated.gif


回答5:

Or if you know a bit of python, then you can easily leverage the help of it from python shell.

Hit up python shell by typing python in your terminal. And apply following magic spells-

# Suppose your files are like 1.jpeg, 2.jpeg etc. upto 100.jpeg
files = []
for i in range(1, 101):
    files.append('{}.jpeg'.format(i))
command = 'convert -delay 10 {} -loop 0 animated.gif'.format(' '.join(files))
from subprocess import call
call(command, shell=True)

Your job should be done!