Making backgrounds of animated GIF files transparent


I have been creating animated GIF files using GIMP from several frames in PNG format that I created using Jmol and now I would like to know how to make the background of the resulting GIF files transparent. Any pieces of software or websites you suggest should be available for free, please.

10/11/2014 7:46:00 PM

Accepted Answer

You can do this with GIFsicle, using the following options:

gifsicle -U --disposal=previous --transparent="#ffffff" -O2 anim.gif > anim_trans.gif

where anim.gif and anim_trans.gif are the source and destination file names, and #ffffff is the hex code of the color you want to make transparent (here, pure white).

(The important options here are -U / --unoptimize and --disposal=previous, which together convert the animation into "flipbook mode", where each frame is fully erased before drawing the next one. This allows extra transparency to be added to the frames without letting the earlier frames show through the transparent parts. The -O2 option is not strictly necessary, but it's likely to shrink the file size of the resulting animation by optimizing the frames to avoid needlessly re-drawing static parts of the animation.)

For a demostration, here's an animation of the human glyoxalase I (GLO1) enzyme from Wikimedia Commons (left), and the same animation with transparency added using the method shown above (right):

Original animation with white background Modified animation with transparent background
Original animation by Wikimedia Commons user WillowW, used under the CC-By 3.0 license.

One detail worth noting is that, if the GIFsicle command doesn't seem to have any effect, you should check that the background color is really correct. For example, for the animation shown above, the actual background color turned out to be #fdfffc (i.e. very slightly yellow-greenish white) rather than #ffffff (pure white). You can't actually tell the difference by eye (or, at least, I can't do that on this screen), but it's enough to make GIFsicle consider the two colors different.

Also note that GIF files only support 1-bit transparency, which means that the edges of the transparent regions will not be anti-aliased. This is OK as long as the background you're showing the animation on isn't too far from the original background color, but if it is, you may find that there will be some ugly color fringing around the edges of the animation. Unfortunately, there's not a lot you can do about that, except to choose a more suitable background or to re-render the animation.

4/13/2017 12:46:00 PM