-->

Python的PIL“IO错误:图像文件截断”大型图片(Python PIL “IOError: i

2019-07-01 10:25发布

我觉得这个问题是不是Zope的相关。 不过我会解释我想要做的事:

我在Zope中使用PUT_factory上传图片到每FTP的ZODB。 上载的图像被保存为新创建的容器对象内部一个Zope图像。 这工作得很好,但我想,如果超过一定尺寸(宽度和高度)来调整图像大小。 所以我使用PIL的缩略图功能,即调整他们200×200。 这只要上传的图片是比较小的正常工作。 我没有检查出确切的限制,但976x1296px仍然是确定。

有了更大的图片,我得到:

Module PIL.Image, line 1559, in thumbnail
Module PIL.ImageFile, line 201, in load
IOError: image file is truncated (nn bytes not processed).

我测试了很多JPEG文件从我的相机。 我不认为他们都被截断。

这里是我的代码:

if img and img.meta_type == 'Image':
  pilImg = PIL.Image.open( StringIO(str(img.data)) )
elif imgData:
  pilImg = PIL.Image.open( StringIO(imgData) )

pilImg.thumbnail((width, height), PIL.Image.ANTIALIAS)

由于我使用的是PUT_factory,我没有一个文件对象,我使用的是从工厂或任何原始数据之前创建(Zope的)图像对象。

我听说PIL超过一定的规模时,不同的方式处理图像数据,但我不知道如何调整我的代码。 或者是它关系到PIL的延迟加载?

Answer 1:

我有点晚在这里回答,但我遇到了类似的问题,我想分享我的解决方案。 首先,这里是这个问题的一个非常典型的堆栈跟踪:

Traceback (most recent call last):
  ...
  File ..., line 2064, in ...
    im.thumbnail(DEFAULT_THUMBNAIL_SIZE, Image.ANTIALIAS)
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1572, in thumbnail
    self.load()
  File "/Library/Python/2.7/site-packages/PIL/ImageFile.py", line 220, in load
    raise IOError("image file is truncated (%d bytes not processed)" % len(b))
IOError: image file is truncated (57 bytes not processed)

如果我们环顾四周线220(201-也许你正在运行一个稍微不同的版本,你的情况线),我们看到,PIL在文件的块并预计该区块将是具有一定规模的阅读。 事实证明,你可以问PIL宽容被截断(失踪块中的某些文件),通过改变设置文件。

某处你的代码块之前,只需添加以下内容:

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

...你应该是不错的。

编辑:它看起来像这样有助于与枕头附带的版本,PIL的(“点子安装枕”),但不得用于PIL的默认安装工作



Answer 2:

最好的是,你可以:

if img and img.meta_type == 'Image':
    pilImg = PIL.Image.open( StringIO(str(img.data)) )
elif imgData:
    pilImg = PIL.Image.open( StringIO(imgData) )

try:
    pilImg.load()
except IOError:
    pass # You can always log it to logger

pilImg.thumbnail((width, height), PIL.Image.ANTIALIAS)

由于,因为它似乎愚蠢的 - 它会像一个奇迹。 如果你的形象已经丢失的数据,这将充满灰色(查看图像的底部)。

注:在Python骆驼情况下使用是气馁,只在类名中使用。



Answer 3:

这可能不是一个PIL问题。 它可能与你的HTTP服务器的设置。 HTTP服务器放在一个上限,将被接受的实体主体的大小。

对于例如,在阿帕奇FCGI,选项FcgidMaxRequestLen确定可以上载的文件的最大尺寸。

请检查您的服务器 - 它可能是被限制上传大小的一个。



Answer 4:

我不得不把TDS版本改为7.2,以防止这种情况发生。 另外随着TDS 8.0版的作品,但我有一些其他问题8.0。



文章来源: Python PIL “IOError: image file truncated” with big images