I have many groups of gif images and in each group one of the images is an animated gif. I'm using ImageMagick 6.9.10-23 Q16 x86_6
on Ubuntu 20.04
with PHP 8.2.12
.
For each group I want to take the images and stack them together into a single gif, preserving the gif animation of whichever image layer is derived from the animated gif. The animated image layer is not in the same position in each group and the animations each have a different number of frames.
This example command correctly layers the images into a single static gif.
convert img1.gif img2.gif img3.gif img4.gif -coalesce -gravity center -background none -compose over -layers merge -layers optimize final.gif
Do you know how I can produce the result pictured in expected.gif
below? Thank you.
Example assets
img1.gif
img2.gif
img3.gif
img4.gif
final.gif
expected.gif
In Imagemagick your need to use -layers composite to combine them as follows (in Unix syntax), but first flatten img1, img2, img4 into one image. Note the NULL: operator that separates the flattened image and the animated image
magick \
\( img1.gif img2.gif -flatten img4.gif -flatten \) \
NULL: \
\( img3.gif -coalesce \) \
-compose over -layers composite x.gif
For Windows, remove the \ from the parentheses and change the end of line character \ to ^
See https://imagemagick.org/Usage/anim_mods/#composite
If you want green over red, then try
convert \
\( img1.gif img2.gif -flatten \) \
NULL: \
\( img3.gif -coalesce \) \
-compose over -layers composite \
NULL: \
img4.gif -compose over -layers composite y.gif
That seems to work for me.