ImageMagick - Making 2 GIFs into side by side GIFs using IM convert ImageMagick - Making 2 GIFs into side by side GIFs using IM convert unix unix

ImageMagick - Making 2 GIFs into side by side GIFs using IM convert


I don't have 2 animated GIFs of the same length, so I'll just use two copies of this one:

enter image description here

Let's look at the frames in there, with this:

identify 1.gif1.gif[0] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[1] GIF 449x339 500x339+51+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[2] GIF 449x339 500x339+51+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[3] GIF 449x339 500x339+51+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[4] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[5] GIF 449x339 500x339+51+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[6] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[7] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[8] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[9] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[10] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[11] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[12] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[13] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[14] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[15] GIF 448x339 500x339+52+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[16] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.0001.gif[17] GIF 500x339 500x339+0+0 8-bit sRGB 32c 508KB 0.000u 0:00.000

Mmmm, 18 frames with different sizes, that means we need to use -coalesce to rebuild partial frames into full ones.

Let's copy that and make 2.gif

cp 1.gif 2.gif

Now we can split the two gifs into their component frames, like this:

convert 1.gif -coalesce a-%04d.gif     # split frames of 1.gif into a-0001.gif, a-0002.gif etcconvert 2.gif -coalesce b-%04d.gif     # split frames of 2.gif into b-0001.gif, b-0002.gif etc

Now let's join the individual frames side-by-side:

for f in a-*.gif; do convert $f ${f/a/b} +append $f; done

Note that ${f/a/b} is a bash-ism meaning "take the value of f and replace the letter 'a' with 'b'".

And put them back together again:

convert -loop 0 -delay 20 a-*.gif result.gif

That looks longer, and harder, than it is because I tried to explain it all, but it looks like this really:

convert 1.gif -coalesce a-%04d.gif                         # separate frames of 1.gifconvert 2.gif -coalesce b-%04d.gif                         # separate frames of 2.giffor f in a-*.gif; do convert $f ${f/a/b} +append $f; done  # append frames side-by-sideconvert -loop 0 -delay 20 a-*.gif result.gif               # rejoin frames

enter image description here

Note that this conceptual code, not production quality. It doesn't remove the temporary files it creates, nor does it carry the inter-frame time forward from the original GIFs. If you want to get the original frame rate you could get them like this and save them into an array and feed the delays back into the re-animation command at the end:

identify -format "%f[%s] %T\n" 1.gif1.gif[0] 81.gif[1] 81.gif[2] 81.gif[3] 81.gif[4] 81.gif[5] 81.gif[6] 81.gif[7] 81.gif[8] 81.gif[9] 81.gif[10] 111.gif[11] 111.gif[12] 111.gif[13] 111.gif[14] 111.gif[15] 111.gif[16] 111.gif[17] 26

Also, you may want a spacer between the two animations, say 10 pixels, which you can do by replacing the convert command inside the for loop with this one:

convert $f -size 10x xc:none ${f/a/b} +append $f

enter image description here