# Can I set the alpha channel in a GIF image?

I have an image in GIF format:

I want to set the alpha channel in this GIF.

gif = Import["http://i.stack.imgur.com/EC0dH.gif"];
chan = Binarize[#, 0.94] & /@ gif // ColorNegate // Blur[#, 2] & /@ # &;
mygif = MapThread[SetAlphaChannel[#1, #2] &, {gif, chan}];
Export["mygif.gif", mygif]


There is a solution to a similar problem given here, but I find it doesn't work for me.

• @m_goldberg Thanks for your edit. – yode Mar 6 '16 at 15:17

The file format .gif uses color palettes, and does not support general alpha channels. But you can select an individual color and specify that color should be interpreted as transparent. Hence, in your case, you want white:

Export["mygif.gif", mygif, "TransparentColor" -> White]


Here's one frame as interpreted by Photoshop (where transparency is shown by the grey boxes)

In response to the query about making the eyes transparent, this can be done by quantizing the color. For example:

Export["mygif.gif", ColorQuantize[gif[[1]], 8],
"TransparentColor" -> RGBColor[228/255, 34/255, 15/255]]


quantizes the bulk of the eyes to a color near red, and then chooses that color to be transparent. Of course, when you do this, the background it no longer transparent. To do that, you could change the reddish pixels to white, and then use "TransparentColor" -> White.

• There are a lot strange.I have try your method before,but it failure.So I post this question in here.Even more,Export["test.gif", gif, "TransparentColor" -> White] work fine.Another question,what meaning of the White?Why do I use Export["test1.gif", gif, "TransparentColor" -> Red],the eyes of this gif is not transparent? – yode Mar 6 '16 at 16:31
• Colors are represented in RGB form, so White is RGBColor[1,1,1], while Red is RGBColor[1,0,0]. The reason you don't get transparent eyes with "TransparentColor"->Red is that the eyes are not a single color, they are many colors near RGBColor[1,0,0]. Thus you need to make sure that all the area you wish to be transparent has exactly the same RGBColor. – bill s Mar 6 '16 at 16:35
• As your promption,Export["test.gif", gif, "TransparentColor" -> RGBColor[{239, 8, 8}/255]] cannot make the eyes transparent still. – yode Mar 6 '16 at 20:55
• In a gif file, you can only make one color (one specific value of RGBColor[ ]) transparent. If you look at the "red" pixels in the eye -- each pixel has a somewhat different value. You might get the eyes transparent if you ColorQuantize first. – bill s Mar 6 '16 at 21:38
• I know your meaning. So you can make the eyes(a part) be transparent? – yode Mar 6 '16 at 22:13