[Image-SIG] Problem reading PNG image

Terry Hancock hancock at anansispaceworks.com
Fri Oct 31 13:52:49 EST 2003

On Friday 31 October 2003 08:05 am, Bob Ippolito wrote:
> On Oct 31, 2003, at 8:59 AM, Kevin D. Smith wrote:
> > I have some images generated by the dvi2bitmap program that don't seem 
> > to work in PIL.  PIL gives me the size correctly, but if I try to do 
> > anything with it (e.g. getbbox), I get an IOError in the load method.  
> > See the attached image as an example.
> I can reproduce this problem (OS X 10.3, PIL compiled with libtiff, 
> libjpeg, libpng, freetype against vendor Python 2.3).  I'm not sure 
> what the solution is, but the OS itself reads the png just fine (I see 
> it inline in your message).

As documented, PIL does *not* support all PNG files, only
non-interlaced ones, IIRC.  The bevavior you described is
consistent with this limitation. PIL doesn't actually try to read
image data immediately, but only when needed -- which is
why you get the error afterwards.  I'm pretty sure that IOError
is in fact what it throws when this happens, though it probably
ought to throw something more descriptive.

Anyway, I have a small collection of PIL-incompatible PNG files
I use for testing failure modes, which gives me, e.g.:

>>> q = p.convert('RGB')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/narya/lib/python2.1/site-packages/PIL/Image.py", line 439, in convert
  File "/usr/local/narya/lib/python2.1/site-packages/PIL/ImageFile.py", line 166, in load
    raise IOError, "decoder error %d when reading image file" % e
IOError: decoder error -3 when reading image file

As I see it, you have three choices:

1) Avoid these types of image (use something else to convert them
to a PIL safe format). Probably easiest, if you have control over which
images are used.

2) Fix PIL's PNG reader so that it does all PNGs (which would, of course,
help more than just you!).

3) Put an error trap in your program and communicate the error (with
perhaps more useful behavior) to the user.  This'll do in a pinch, if you
need it to work generally -- and unsupported PNG format images aren't
the only unreadable image you might run into.

Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

More information about the Image-SIG mailing list