python - PIL处理PNG的问题

标签 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 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

最佳答案

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

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

关于python - PIL处理PNG的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/919104/

相关文章:

c++ - 无法将PNG图像保存到文件中

python - 如何从 Python 中的 .gif 动画中提取给定帧

python - 在 MacOS 上使用 OpenCV 可以轻松将 jpg 转换为 bmp。可以用枕头来做这项工作吗?

python - 如何确定数据是否是没有文件的有效 tar 文件?

python - App Engine ndb StringProperty 和字符串哈希

java - 应用程序显示错误的绘图

ios - UIImage 到 CIImage 到 UIImage 导致 pngData 在 iOS 12 上返回 nil

python - 如何在 Python 中从蒙版分割图像创建轮廓(粗细可控)?

python - 在 Spyder 中再次运行代码后如何查看更新的数据框(每次运行后无需从变量资源管理器中双击)?

python - `tf.contrib.layers.max_pool2d` 和 `tf.nn.max_pool` 和有什么本质区别?