Python PIL“IOError:图像文件被截断”有大图像 [英] Python PIL "IOError: image file truncated" with big images

查看:680
本文介绍了Python PIL“IOError:图像文件被截断”有大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题与Zope无关。尽管如此,我还是会解释我要做的事情:

I think this problem is not Zope-related. Nonetheless I'll explain what I'm trying to do:

我在Zope中使用PUT_factory将图像上传到每个FTP的ZODB。上传的图像将作为Zope Image保存在新创建的容器对象中。这工作正常,但如果图像超过一定的大小(宽度和高度),我想调整图像的大小。所以我使用PIL的缩略图功能来调整它们的大小,即200x200。只要上传的图像相对较小,这就可以正常工作。我没有查看确切的限制,但976x1296px仍然可以。

I'm using a PUT_factory in Zope to upload images to the ZODB per FTP. The uploaded image is saved as a Zope Image inside a newly created container object. This works fine, but I want to resize the image if it exceeds a certain size (width and height). So I'm using the thumbnail function of PIL to resize them i.e. to 200x200. This works fine as long as the uploaded images are relatively small. I didn't check out the exact limit, but 976x1296px is still ok.

我得到更大的图片:

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

我从相机测试了很多jpeg。我不认为它们都被截断了。

I tested a lot of jpegs from my camera. I don't think they are all truncated.

这是我的代码:

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)图像对象。

As I'm using a PUT_factory, I don't have a file object, I'm using either the raw data from the factory or a previously created (Zope) Image object.

我听说PIL在超过一定大小时处理图像数据的方式不同,但我不知道如何调整代码。或者它与PIL的延迟加载有关吗?

I've heard that PIL handles image data differently when a certain size is exceeded, but I don't know how to adjust my code. Or is it related to PIL's lazy loading?

推荐答案

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

I'm a little late to reply here, but I ran into a similar problem and I wanted to share my solution. First, here's a pretty typical stack trace for this problem:

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容忍被截断的文件(从块中丢失一些文件)。

If we look around line 220 (in your case line 201—perhaps you are running a slightly different version), we see that PIL is reading in blocks of the file and that it expects that the blocks are going to be of a certain size. It turns out that you can ask PIL to be tolerant of files that are truncated (missing some file from the block) by changing a setting.

在您的代码块之前的某个地方,只需添加以下内容:

Somewhere before your code block, simply add the following:

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

...你应该是好的。

...and you should be good.

编辑:看起来这有助于Pillow捆绑的PIL版本(pip install pillow),但可能无法用于PIL的默认安装

It looks like this helps for the version of PIL bundled with Pillow ("pip install pillow"), but may not work for default installations of PIL

这篇关于Python PIL“IOError:图像文件被截断”有大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆