PIL 处理 PNG 时出现问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于 PIL 中 close 编码不正确造成的,它是一个错误。
编辑文件(系统上的路径可能不同):
sudo vi /usr/lib64/python2.6/site-packages/PIL/ImageFile.py
在线283修改:
更改为:
就是这样,注释掉损坏的代码,添加正确的行,然后保存文件。 一切都完成了,只需运行之前失败的程序,它现在就可以运行了。
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:
Change it to:
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.