1

I have a series of images with the file names

motorist_intensity_2.jpg
 ...
 ...
motorist_intensity_256.jpg

ad I want to make an animated gif from them using ImageMagick. Now, the command

convert -delay 100 -loop 0 motorist_intensity_* motorist.gif

works, but the frames are out of sorted order. I can produce a sorted file list using

ls motorist_intensity_* | sort -n -t _ -k 3

but how can I pass that list to the covert commad in place of the origial globmotorist_intensity_*?

Jason
  • 11,263
  • 21
  • 87
  • 181

2 Answers2

4

You can use brace expansion to expand in the order you want:

convert -delay 100 -loop 0 motorist_intensity_{2..256}.gif motorist.gif
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • If you know the numbers in advance, and there are no frames missing, then this is indeed a very nice solution. – Floris Jan 30 '14 at 16:37
  • 1
    Unfortunately, it assumes that all numbers between the start and end exist. If they don't, you get a wall of Imagemagick error output. – Jason Jan 30 '14 at 16:44
3

The following should work:

convert -delay 100 -loop 0 `ls motorist_intensity_* | sort -n -t _ -k 3` motorist.gif

By putting the command ls ...etc in back quotes, it gets executed and the output is inserted

update

It is preferable to use the $() technique for evaluating an expression (see comments below); also, I am not 100% sure that the output of sort won't include newlines that mess you up. Solving both problems:

convert -delay 100 -loop 0 $(ls motorist_intensity_* | sort -n -t _ -k 3 | xargs echo ) motorist.gif

The xargs echo is a nice shorthand for "Take each of the lines of output of the input in turn, and echo them to the output without the newline". It is my preferred way of converting multiple lines to a single line (although there are many others).

PS - This does not solve the problem you would get if the filename contained a newline. I am assuming they don't...

Floris
  • 45,857
  • 6
  • 70
  • 122
  • I don't think that parsing `ls` is specially [a good idea](http://mywiki.wooledge.org/ParsingLs). Also, it is always better to use the `$()` syntax. – fedorqui Jan 30 '14 at 16:25
  • @fedorqui - can you explain why `$()` is better than `\` \``? I was trying to stay close to the original post - and just focus on the question "how do I get the output of X into Y". By the way - thanks for the link. I didn't know about the dangers of parsing `ls` output. – Floris Jan 30 '14 at 16:27
  • 1
    Sure, `` are deprecated, as read in http://stackoverflow.com/a/4708569/1983854 for example. – fedorqui Jan 30 '14 at 16:28
  • 1
    The "easier to nest" reason is very compelling. Thanks for the education. I learn something new every day. Today, this was it. – Floris Jan 30 '14 at 16:30