Merging 2 gifs, one is animated and transparent

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
av01d
Posts: 6
Joined: 2013-10-02T03:24:45-07:00
Authentication code: 6789

Re: Merging 2 gifs, one is animated and transparent

Post by av01d »

Reviving an old thread... I have a question similar to what was asked above.
I have a JPEG image of say 400x300px. I have a small animated GIF of say 100x50px.
I want to overlay the animated GIF over the JPG, which should result in an animated GIF. The GIF should have the same width/height as the JPEG image (400x300px in this particular example).

I tried:

Code: Select all

convert smallAnim.gif -coalesce -draw "image DstOver 0,0 0,0 background.jpg" -layers Optimize output.gif
Which is close to what I want: output.gif shows the animation (smallAnim.gif) over the background image (background.jpg), and it animates correctly, but the resulting image has the width/height of smallAnim.gif. It's too small.

When I want to position the animation to a certain x,y coordinate:

Code: Select all

convert smallAnim.gif -coalesce -draw "image DstOver 150,125 0,0 background.jpg" -layers Optimize output.gif
It gets worse; output.gif only shows the animation, the background image is not visible.

How can I make this work properly, without reverting to merging the images frame-by-frame (which works, but is cumbersome).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merging 2 gifs, one is animated and transparent

Post by snibgo »

Testing and suggesting a solution is far easier if you supply sample input files. You should also say what version IM you use, on what platform.

The problem is that IM uses the first input as metadata (including dimensions) for the output. You might use a dummy first input:

Code: Select all

convert background.jpg smallAnim.gif -delete 0 -coalesce -draw \"image DstOver 0,0 0,0 background.jpg\" -layers Optimize output.gif
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Merging 2 gifs, one is animated and transparent

Post by anthony »

This is covered by IM Examples!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
av01d
Posts: 6
Joined: 2013-10-02T03:24:45-07:00
Authentication code: 6789

Re: Merging 2 gifs, one is animated and transparent

Post by av01d »

I managed to figure it out myself:

Code: Select all

convert background.jpg null: \( smallAnim.gif -coalesce \) -geometry +150+125 -layers Composite -layers Optimize output.gif
Works like a charm. Is this indeed what I should be using?

The suggestion made by snibgo (thanks!) does not seem to work for me...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Merging 2 gifs, one is animated and transparent

Post by anthony »

Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply