GIF89A and PIL

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Sat Mar 27 11:17:46 EDT 2010


Stephen Hansen <apt.shansen at gmail.invalid> writes:

> Is it possible to get PIL to save GIF's in GIF89A format, instead of
> GIF87A?

GIF89 was patented. I guess that is why it isn't used by PIL. (The
patent has expired now, IIRC.) Anyway, PNG was supposed to replace GIF.

> If not, are there any decent other image libraries out there
> that anyone's familiar with? The only one I could find was
> PythonMagick, which seems completely undocumented. Or I'm blind.

I don't know PythonMagick, but it is based on ImageMagick, which is kind
of a swiss-knife in image manipulation and conversion. You could try the
standalone tools first, to see if you get what you want/need.

> But the problem is, I have a nice, small little 72 byte GIF89A file,
> that I want to do some slight tweaking on and then re-save. I noticed
> that even if I completely skipped the tweaking step and -just- saved,
> it exploded into a 919 byte GIF87A file. And in this context, bytes
> really, really matter. I picked GIF over PNG because the same file in
> PNG was 120 bytes :)
[...]
>>>> f = open('../tiles/BaseTemplate.gif', 'rb')
>>>> d1 = f.read()
>>>> len(d1)
> 73
>>>> d1
> 'GIF89a\x10\x00\x10\x00[...]'

Hmm, a 16x16 image. Don't expect much from the most sophisticated
formats (e.g, PNG), because their overhead (headers etc.) may well be
above the size of the data. Compression isn't usually targeted at small
files.

(BTW: "slight tweaking" may have an effect on file-size if it introduces
new colors, because GIF uses a color-table. I guess you know all this.)

GIF uses the LZW algorithm, and so does zip and gzip (the latter with an
additional layer of Huffmann coding). If your images are of fixed size,
you _may_ be better off compressing the raw data with a general purpose
compressor (i.e., gzip). Check the packages gzip and zlib.

-- Alain.



More information about the Python-list mailing list