PIL 处理 PNG 时出现问题

发布于 2024-07-22 00:58:57 字数 921 浏览 6 评论 0原文

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
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")

It always get stuck in here:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-07-29 00:58:57

这是由于 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

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

This results from an incorrect coding of close within PIL, its a bug.

Edit the File ( path may be different on your system ):

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

Online 283 Modify:

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

Change it to:

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

Thats it, comment out the broken code, add the correct line, and save the file. All done, just run the program that was failing before and it will work now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文