【问题标题】:Problem in handle PNG by the PILPIL处理PNG的问题
【发布时间】:2010-10-29 11:30:38
【问题描述】:
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")

它总是卡在这里:

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

【问题讨论】:

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

标签: python png python-imaging-library


【解决方案1】:

这是由于 PIL 中的 close 编码不正确,这是一个错误。

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

sudo vi /usr/lib64/python2.6/site-packages/PIL/ImageFile.py

在线283修改:

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

改为:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2017-03-12
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多