[Image-SIG] Re: fully working PIL binary?

Fredrik Lundh fredrik@pythonware.com
Mon, 5 May 2003 13:35:01 +0200

Lawrence W. Leung wrote

> I've been using PIL to save JPEGs and the code I have crashes 1.1.2 on
> save and causes a return code of -2 on 1.4.1b1's save.  (I can't get
> 1.1.3 compiled properly) I've been using
> the python 2.2.2 windows build (official build from the website) and the
> official binaries of PIL.
> Anyone know how I can solve this problem?

are you using the "optimize" flag?

the JPEG encoder may fail during optimization in some circumstances,
due to lack of buffer space.  possible workarounds (use both, to be
on the safe side):

- set the ImageFile.MAXBLOCK value to a bigger value (larger
than the largest JPEG file you plan to write, but not larger than
the amount of memory you have on your computer):

    import ImageFile
    ImageFile.MAXBLOCK = 16777216

- fall back on non-optimized writing if save-with-optimize fails:

        im.save(filename, optimize=1)
    except IOError:
        # retry w/o optimization

(if you're saving to an output stream, use a StringIO buffer to
avoid writing partial files to the stream).