Search code examples
imagemagick

ImageMagick `-duplicate` but to beginning of sequence


-duplicate

convert img*.png -duplicate 3 out.gif

makes

0 1 2 2 2 2

where 0 is img0.png, and 1 is img1.png, etc, making a GIF. But can I make below?

0 0 0 0 1 2 2 2 2

that is, append to end and start? I get I can use indexing to make

0 1 2 2 2 2 0 0 0

which is identical in a loop, but I need the 0s at start in context.


Solution

  • TL;DR for 0 0 0 1 2 3 ... 20 21 21 21 (any number of images) do

    convert img*.png -write mpr:imgs -delete 0--1 mpr:imgs[0,0,0,1--1,-1,-1] out.gif
    

    Note: avoid extra commas, e.g. mpr::imgs[1--1,] inserts frame 0 (which isn't even selected!) as last frame. Also, convert is "legacy' now, magick is current, and it's a drop-in replacement for this particular command (and there's a porting guide).


    Using ImageMagick v6 on Windows command line, this command will let you arrange the order and number of images in any way you need.

    convert img*.png -write mpr:imgs -delete 0--1 mpr:imgs[0,1,2,2,2,2,0,0,0] out.gif
    

    That reads the 3 input images, copies them all into a memory register named "mpr:imgs", deletes the input images from the command, then reads the images from that memory register as you specify in the square brackets. The image index 0 is the first image read into the command, 1 is the second, etc.

    Also, using -duplicate you can specify which image in the list you want to use according to their order in the list. This command will give the same result as the one above...

    convert img*.png -duplicate 3 -duplicate 3,0 out.gif
    

    The command reads the three images, then -duplicate 3 makes 3 more of the last image in the list, then -duplicate 3,0 makes 3 more of the first image in the list, index 0.

    Another approach would be to use ( -clone ... ) inside parentheses to create the number of duplicates in the order you want.

    convert img*.png ( -clone 2,2,2 -clone 0,0,0 ) out.gif
    

    That reads the 3 input images, clones the third image 3 times, then clones the first image 3 times, giving the same result as the commands above.

    These commands are in Windows syntax. For a *nix OS you'd have to escape the parentheses with backslashes "\(...\)".

    Also helpful are -swap, -reverse, -insert, and -delete to manipulate the order of images in the list.