我认为这个问题与 Zope 无关。尽管如此,我会解释我正在尝试做的事情:
我在 Zope 中使用 PUT_factory 通过 FTP 将图像上传到 ZODB。上传的图像在新创建的容器对象中保存为 Zope 图像。这很好用,但是如果图像超过一定大小(宽度和高度),我想调整它的大小。所以我使用 PIL 的缩略图功能来调整它们的大小,即 200x200。只要上传的图像相对较小,它就可以正常工作。我没有检查确切的限制,但 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) Image 对象。
我听说当超过一定大小时 PIL 会以不同的方式处理图像数据,但我不知道如何调整我的代码。还是和PIL的延迟加载有关?
最佳答案
我在这里回复有点晚了,但我遇到了类似的问题,我想分享我的解决方案。首先,这里是这个问题的一个非常典型的堆栈跟踪:
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 正在读取文件的 block ,并且它预计这些 block 将是一定大小。事实证明,您可以通过更改设置来要求 PIL 容忍被截断的文件(从 block 中丢失一些文件)。
在代码块之前的某处,只需添加以下内容:
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
...你应该很好。
编辑:看起来这有助于 PIL 与 Pillow 捆绑的版本(“pip install枕头”),但可能不适用于 PIL 的默认安装
关于python - 带有大图像的 PIL "IOError: image file truncated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984426/