1
$\begingroup$

I have an image in GIF format:

enter image description here

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.

$\endgroup$
  • $\begingroup$ @m_goldberg Thanks for your edit. $\endgroup$ – yode Mar 6 '16 at 15:17
3
$\begingroup$

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)

enter image description here

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – yode Mar 6 '16 at 16:31
  • $\begingroup$ 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. $\endgroup$ – bill s Mar 6 '16 at 16:35
  • $\begingroup$ As your promption,Export["test.gif", gif, "TransparentColor" -> RGBColor[{239, 8, 8}/255]] cannot make the eyes transparent still. $\endgroup$ – yode Mar 6 '16 at 20:55
  • $\begingroup$ 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. $\endgroup$ – bill s Mar 6 '16 at 21:38
  • $\begingroup$ I know your meaning. So you can make the eyes(a part) be transparent? $\endgroup$ – yode Mar 6 '16 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.