[Image-SIG] Problem with parsing PNG image

Fredrik Lundh fredrik at pythonware.com
Thu Nov 8 22:06:16 CET 2007


Christian Joergensen wrote:

> I'm having trouble parsing a PNG image. This snippet should show my problem:
> 
> from PIL import ImageFile
> import StringIO
> import urllib2
> f = 
> urllib2.urlopen(urllib2.Request(url='http://www.bold.dk/billeder/art8651.png'))
> p = ImageFile.Parser()
> p.feed(f.read())
> im = p.close()
> output = StringIO.StringIO()
> im.save(output, format="image/png")
> 
> I keep getting:
> 
>    File "/usr/lib/python2.4/site-packages/PIL/ImageFile.py", line 298, 
> in read
>      data = self.data[pos:pos+bytes]
> TypeError: unsubscriptable object
> 
> Am I doing something wrong?

well, the code looks right (except for the format string syntax; use 
"PNG", not a mime type), and I'm getting the same error, so it's more 
likely to be a bug.  a 5-minute inspection didn't reveal any obvious 
work around, so I'm afraid you probably have to replace the parsing with:

     f = urllib2.urlopen(...)
     im = Image.open(StringIO.StringIO(f.read()))

</F>



More information about the Image-SIG mailing list