Python PIL处理PNG时出现问题

Python PIL处理PNG时出现问题,python,png,python-imaging-library,Python,Png,Python Imaging Library,它总是卡在这里: from PIL import ImageFile as PILImageFile p = PILImageFile.Parser() #Parser the data for chunk in content.chunks(): p.feed(chunk) try: image = p.close() except IOError: return None #Here the model is

它总是卡在这里:

from PIL import ImageFile as PILImageFile

p = PILImageFile.Parser()

#Parser the data
for chunk in content.chunks():
    p.feed(chunk)    
try:
    image = p.close()
except IOError:                        
    return None
#Here the model is RGBA
if image.mode != "RGB":
    image = image.convert("RGB")

这是由于PIL中close的编码不正确造成的,这是一个bug

编辑文件(系统上的路径可能不同):

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None
sudo vi/usr/lib64/python2.6/site-packages/PIL/ImageFile.py

在线283修改:

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None
将其更改为:

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None

就是这样,注释掉断开的代码,添加正确的行,然后保存文件。全部完成后,只需运行以前失败的程序,现在就可以工作。

这是由于PIL中close的错误编码导致的,这是一个错误

编辑文件(系统上的路径可能不同):

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None
sudo vi/usr/lib64/python2.6/site-packages/PIL/ImageFile.py

在线283修改:

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None
将其更改为:

image = image.convert("RGB")

File "C:\Python25\Lib\site-packages\PIL\Image.py" in convert
  653.         self.load()
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
  189.                     s = read(self.decodermaxblock)
File "C:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in load_read
  365.         return self.fp.read(bytes)
File "C:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
  300.             data = self.data[pos:pos+bytes]

Exception Type: TypeError at 
Exception Value: 'NoneType' object is unsubscriptable
def close(self):
    self.data = self.offset = None

就是这样,注释掉断开的代码,添加正确的行,然后保存文件。全部完成,只需运行以前失败的程序,现在就可以运行。

对于其他类型的图片,例如jpg等,它没有此类问题。对于其他类型的图片,例如jpg等,它没有此类问题。