PIL's ImageFile Parser and PNG's

Silfheed silfheed at gmail.com
Thu Nov 13 17:09:13 EST 2008


On Nov 12, 6:04 pm, Silfheed <silfh... at gmail.com> wrote:
> On Nov 12, 5:28 pm,Silfheed<silfh... at gmail.com> wrote:
>
>
>
> > Heyas
>
> > So I've been messing around with the PIL and PNG's and came across a
> > little problem with PNG's.
>
> > So just to clarify, I'm running with the standard ubuntu 8.04 python-
> > imaging package that installs zlib and all the other goodies that go
> > along to make the PIL work with PNG's nicely.
>
> > So this works fine:
>
> >  from PIL import Image
> >  x = Image.open('some.png')
> >  x.show()
>
> > But if I try to get  a bit fancier and do
> >  from PIL import ImageFile
> >  p = ImageFile.Parser()
> >  p.feed(open('some.png','rb').read())
> >  image = p.close()
> >  image.show()
>
> > I get a traceback:
> > Traceback (most recent call last):
> >   File "basic_img.py", line 9, in <module>
> >     image.show()
> >   File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1450, in
> > show
> >     _showxv(self, title, command)
> >   File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 2089, in
> > _showxv
> >     file = image._dump(format=format)
> >   File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 477, in
> > _dump
> >     self.load()
> >   File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 189,
> > in load
> >     s = read(self.decodermaxblock)
> >   File "/usr/lib/python2.5/site-packages/PIL/PngImagePlugin.py", line
> > 365, in load_read
> >     return self.fp.read(bytes)
> >   File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 300,
> > in read
> >     data = self.data[pos:pos+bytes]
> > TypeError: 'NoneType' object is unsubscriptable
>
> > But if all I do is change some.png to some.jpg (or gif), it works
> > fine.   Am I doing something horribly wrong, or forget to compile
> > something that was supposed to be compiled for PNG support?  Is it a
> > known bug with a fix somewhere that I just haven't found (I've
> > searched all over the PIL site)
>
> > Thanks
>
> oh and forgive the errant 's (as in PNG's...)


Well, I dunno how much of a valid fix it is, but in ImageFile.py,
simply changing _ParserFile's close() to read:
 def close(self):
  self.offset = None

as opposed to

 def close(self):
  self.data = self.offset = None

allows png's to be loaded correctly for me.



More information about the Python-list mailing list