[Image-SIG] Transparent Gif

Takekun Sato too@pk.highway.ne.jp
Tue, 17 Apr 2001 07:29:33 +0900


Hello, I have posted 2 articles about gif with titles of
 "Gif file size too big for 1x1 pixel image" and "Transparent Gif"
recently.

I think I have solved the problems by modifying "GifImagePlugin.py".

As to making transparent gif, you can consult a message here.
http://mail.python.org/pipermail/image-sig/1999-December/000949.html


- how to set palette size of saved gif

(I'm not sure it is good way, so tips are presented here
 with no guarantee.
If you find something wrong, or better way
to do the same thing, please send me an email.)

   > im=Image.new("P",(1,1),0);
   > im.putpalette([0,0,0,255,255,255]);
   > im.putpixel(1);
   > im.info["colors"]=2;  #set palette size(extension)
   > im.save("test.gif");

   (A saved image thus was 38Bytes, initially about 800Bytes.)

   Before testing above,
   you need to modify "GifImagePlugin.py" like following.

    (From "GifImagePluglin.py" around line 295)
    # global palette
    if im.mode == "P":
        # colour palette
        ### s.append(im.im.getpalette("RGB"))      #1. comment line 301
        #-------------insertion start-----------#  #2. add following
        palstr=im.im.getpalette("RGB");
        while(1):
            if(im.info.has_key("colors")):
                cols=im.info["colors"]; bits=None;
                if(cols<=0 or cols>256):
                     raise ValueError,"colors(%s) out of range[1:256]
!"%cols
                for i in range(1,8):
                    if(2**i>=cols): bits=i; break;
                if(bits!=None):
                    if(len(palstr)>(3*cols)):
                        palstr=palstr[:(3*(2**bits))];

s=["GIF87a"+o16(im.size[0])+o16(im.size[1])+chr((bits-1)+128)+chr(0)+chr(0),palstr];

                        break;
            s.append(palstr);
            break;
        #--------------insertion end--------------#
    else:
        # greyscale
        for i in range(256):
            s.append(chr(i) * 3)