Merging Animated GIFs as Layers

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
avs84

Merging Animated GIFs as Layers

Post by avs84 »

Hello,

I have been trying to find a solution to this problem for 2 days already :shock: . A will appreciate any help here.

I have to combine 2 animated gif files with different frames number and different duration into a single animated files with a fixed duration and infinite loop. They have to be layered one above the other.
One of the images has less frames than the other, when the shorter finishes its animation it has to display the last frame until the other animation finishes and then the whole animation repeats.
I need the command to be able to work on any two random animations I decide to combine.

For example I have these two gifs here:
ImageImage
I managed to come here so far:
Image
I will really appreciate any help here. Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging Animated GIFs as Layers

Post by fmw42 »

The solution was posted just a few days ago in this very forum. See Anthony's solution for the post at viewtopic.php?f=1&t=12573&p=50397&hilit ... mes#p50397
avs84

Re: Merging Animated GIFs as Layers

Post by avs84 »

Thanks! This solved my problem.

However, is there other method than using -clone ? I thought there might be built-in command for this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging Animated GIFs as Layers

Post by fmw42 »

Not that I know about. You need to separate frames using -coalesce and then repeat frames with -clone to duplicate enough frames to have the same number of frames, set appropriate delays, then recombine the animation. Nothing automatic that I know about.

But I will defer to the IM experts on this as I do not do many animations.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Merging Animated GIFs as Layers

Post by anthony »

The ultimate solution to merging two animations is given as a 'psuedo-code' proceedure in...

Merging Time Disjoint Animations
http://www.imagemagick.org/Usage/anim_mods/#merge

The link to the discussion was applying this method to animations with periods of
particularly fast modifications and sections of no changes.

The key is to make the animations of the same length in time (maybe looping one animation number times, and adjusting the timings a bit. AND with the same number of frames, usually by cloning.

The solution that discussion comes up with is NOT GENERAL, but works by converting BOTH animations into a sequence where all frames have a equal time delays, and same number of frames. Sort of like converting each animation into a movie of constant frames per second.

When the timings and number of frames of both animations, you can then use -layers composite to merge the animation, and can even then use other image operators such as -layers RemoveDups to remove frames that don't change after the merger.

See Remove Duplicate Frames - merging consecutive duplicate images
http://www.imagemagick.org/Usage/anim_opt/#removedups


In summery. it is a LIMITED SOLUTION, but one that works well for most 'fast changing' animations.

A script was created but not released to me or to the general public.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply