Delete or exchange background color of 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Delete or exchange background color of animated gif?

Post by anthony »

All three images contain 'shadow effects'.

these are the gray regions under or around the main object in the image, and those are tied to the background color.

Basically if you want to make the white background transparent, the 'shadow effects' would have to be made semi-transparent, which is NOT posible to do in GIF images.

GIF animations with shadow effects MUST be tied into some specific background or background color, or dithered in some way to allow the background to show through.

See IM Examples, Common File Formats, GIF, Background
http://www.imagemagick.org/Usage/formats/#bgnd
for just ONE frame of a GIF.

And Semi-transparency handling for GIF animation Optimization
http://www.imagemagick.org/Usage/anim_o ... itrans_opt
for various dithering methods you can use to handle.

You may especially like to look at Ordered Dithering Techniques
http://www.imagemagick.org/Usage/quanti ... red-dither

Now for extracting the shadow effect from the white background.
There are a number of ways. But as you are doing this on white, I can say you will have a easier time.

Basically mask out the 'opaque areas' then convert shadows di transparency and color them black. This is similar to the non-white double-thresholding method given in
http://www.imagemagick.org/Usage/channe ... antialised

Though it is simpler, I have not created an example for it yet.



In Summery, you will have to figure out how you want to handle 'shadow effects', the best method being to tie the animation to a specific background color or a rough background pattern.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Delete or exchange background color of animated gif?

Post by anthony »

Note the other problem you have is you have not re-optimized the separate images for transparency.

Add a -layers optimize before saving the modified images. And for completeness with 'wierder' GIF animations add a
-layers coalesce after reading the animation!

convert 36_11_7.gif -layers coalesce -background "#cccccc"
-fuzz 15% -draw "fill none matte 0,0 floodfill"
-transparent-color "#ffffff" -layers optimize test_output.gif

see IM Examples, Animation Basics for an understanding of these options, and the first few examples of Animation Modifications as to why you need them.

Basically read the three sections on GIF animations, if you really want to work with animations, so that you will know what you are doing!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply