[Image-SIG] Corrupted GIF output when dimensions are small

Paul Winkler pw_lists at slinkp.com
Fri Nov 21 15:06:07 EST 2003


I can repeatably generate corrupted GIFs by giving
very small dimensions. PNG works fine at the same sizes.
Demonstration follows, simplified a bit from my real app.
I've run this with both python 2.1.3 and 2.2.3, with the same results.


import PIL.Image, PIL.ImageDraw

class maker:
    def _normalize_color(self, colorstring):
        colorstring = colorstring.strip()
        if colorstring[0] == '#': colorstring = colorstring[1:]
        # for some reason, PIL numeric colors seem to be 0xBBGGRR 
        # instead of 0xRRGGBB
        colorstring = colorstring[-2:] + colorstring[2:4] + colorstring[:2]
        color = int(colorstring, 16)
        return color

    def generateImages(self, id='bkg.gif', width=20, height=2,
                       left_color="#FFCC00", right_color="#3366CC", 
                       bkg_color="#FFFFFF"):
        bkg_color = self._normalize_color(bkg_color)
        bkg_img = PIL.Image.new(mode='RGB', size=(width, height),
                                color=bkg_color)
        draw = PIL.ImageDraw.Draw(bkg_img)
        draw.line(((0, 0), (0, height)), fill=left_color)
        draw.line(((width-1, 0), (width-1, height)), fill=right_color)
        # Indexed, no dither.
        bkg_img = bkg_img.convert('P', dither=PIL.Image.NONE, 
	                           palette=PIL.Image.ADAPTIVE)
        fol = '/tmp'
        self._save_image(fol, id, 'background', bkg_img)
        del(draw) 

    def _save_image(self, folder, id, dummy, img):
        import os.path
        where = os.path.join(folder, id)
        img.save(where) 
	
if __name__ == '__main__':
    m = maker()
    color = '#FFFFFF'
    m.generateImages(id='bkg_white.gif', bkg_color=color) #BAD
    m.generateImages(id='bkg_white.png', bkg_color=color) #OK
    m.generateImages(id='bkg_white2.gif', bkg_color=color, 
                     height=5) # OK ... anything over 5 is OK


The 'bad' image, bkg_white.gif, is bad in several ways:

1) Gimp displays it but the vertical lines at left and right do not
extend down the whole height of the image as they should.
They look fine in the other two images.

(I wondered if i should be drawing the lines from 0 to height-1 
instead of 0 to height, which seems more sensible to me, and 
consistent with the treatment of width; but it doesn't do what I expect 
- that just makes the vertical lines stop one pixel short of the edge in all
output formats.)


2) Gimp complains on loading:

GIF: too much input data, ignoring extra...
GIF: bogus character 0x00, ignoring

3) imagemagick's "convert" refuses to convert it:

$ convert /tmp/bkg_white.gif /tmp/bkg_white_converted.gif
convert: Corrupt GIF image (/tmp/bkg_white.gif).

It's odd that increasing the height of the image above 5 fixes the gif.
Further experimentation suggests that I get bad output with the
above method for ANY gif of height < 5 OR width < 2.

Of course, my application is required to generate a 1200x2 gif :-P
If it were up to me I'd just use PNG, but it's not up to me...


Any idea how to fix this? The GIF stuff for PIL appears to all
be in C and I have no idea where to start.

-- 

Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's AMAZING SHOCK COW!
(random hero from isometric.spaceninja.com)



More information about the Image-SIG mailing list