add background on animated gif

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
PeterO
Posts: 1
Joined: 2013-07-23T11:12:32-07:00
Authentication code: 6789

add background on animated gif

Post by PeterO »

Banner:
Image

Worm:
Image

When i add them together, the resulting image has a lot of Kb. (527 Kb)

Code: Select all

convert banner.gif null: worm.gif -layers composite -set dispose background -layers optimizePlus result.gif
When i use a white background it has the expected Kb (32 Kb)

Code: Select all

convert ( banner.gif -bordercolor #ffffff -border 0 ) null: worm.gif -layers composite -set dispose backgroun
d -layers optimizePlus test_out.gif
Does anybody has a clue how can i get the image with a transparent background and about 32Kb size?

Peter
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: add background on animated gif

Post by fmw42 »

PeterO wrote:
Does anybody has a clue how can i get the image with a transparent background and about 32Kb size?

Peter
I tried adding -layers optimizeTransparency, but that did not seem to help.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: add background on animated gif

Post by anthony »

If adding a static color background (white, transparency etc) the image will compress (very very well), however a non-static background will not compress very well.

However with the example
1/ You should ensure the animation is 'coalasced' before composition to remove any existing optimisations
2/ Use -layers Optimize before saving. That should include both frame optimization, and transparency optimization
3/ you do not need to set dispose is you are optimizing as that assumes the image is already coalesced.

I do not believe OptimizePlus will help this animation very much.

Code: Select all

convert banner.gif null: \( worm.gif -coalesce \) \
            -layers composite -layers optimize result.gif
BUT something is failing somewhere! as it is setting a disposal of 'none' when it is clear that the disposal of a should be background for at least some of the frames.

So this is working, though it is not optimized

Code: Select all

convert banner.gif null: \( worm.gif -coalesce \) \
             -layers composite -set dispose background result.gif

Something in the low level functions is really going wrong!

however after a bit of play and simple image warping I get this..

Code: Select all

   convert worm_banner.gif -trim +repage -resize 220x220\! null: \
               \( worm.gif -coalesce -gravity south -chop 0x1 \
                   -background none -wave 13x200 -shear 0x-4 \) \
               -gravity center -geometry +1-16  -layers composite \
               -layers optimizeframe worm_result.gif
Image
No optimization problems due to clean (from resize) and static transparency.

More exact image warping can be done by using a hand created distortion map.

Or perhaps a different order of rotations, wave and shears will get the worm to follow the banner more closely.
To at wave applied at an angle, rotate the image then apply wave, and rotate shear into place. What I did was just a quick 'try until you get reasonably close' iteration.

Alturnativally, leave the background, and resize - distort and mask the work onto the banner (-compose ATop). that way edges of the banner is left as is, worm animation is clipped to the banner, and as it is not involving boolean transparency, you get good anti-aliased edges to the worm!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: add background on animated gif

Post by fmw42 »

Anthony wrote:If adding a static color background (white, transparency etc) the image will compress (very very well), however a non-static background will not compress very well.
This seems to work (IM 6.8.6.6 Q16 Mac OSX) and results in 45 KB file size down from 451 KB

convert banner.gif null: \( worm.gif -coalesce \) \
-gravity center -layers composite -background black -alpha background \
-layers optimize result.gif
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: add background on animated gif

Post by anthony »

Problem seem to happen when you did not 'fix' the hidden transparency color!
It should not depend on transparency color! But it is!!!

Also see the update to my previous post!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: add background on animated gif

Post by fmw42 »

anthony wrote:Problem seem to happen when you did not 'fix' the hidden transparency color!
It should not depend on transparency color! But it is!!!

Also see the update to my previous post!
Neat improvement!

On my Safari browser, it blinks black every full cycle.

But this animation is over 600 KB, so you should fix the base color under the transparency to fix this for now so a smaller file size.


convert banner.gif -background white -alpha remove -trim +repage -resize 220x220\! \
null: \
\( worm.gif -coalesce -gravity south -chop 0x1 -background none -wave 13x200 -shear 0x-4 \) \
-gravity center -geometry +1-16 -layers composite \
-set dispose background -background black -alpha background -layers optimize result.gif

Cuts the file size to 74 KB and does not seem to blink for me.
Post Reply