本文介绍了Python图像库错误-渲染时捕获IOError:数据不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网站,该网站使用sorl-thumbnail调整上传图片的大小.大部分图像的大小调整没有问题,但很少出现以下错误:

I have created a website which is using the sorl-thumbnail to resize the images thats uploaded. Most of the images are getting resized without any issues but for few am getting the following error :

Caught IOError while rendering: not enough data
Request Method: GET
Request URL:    http://localhost:8000/user/nash22/photographs/
Django Version: 1.3.1
Exception Type: TemplateSyntaxError
Exception Value:
Caught IOError while rendering: not enough data
Exception Location: /usr/local/lib/python2.7/site-packages/PIL/TiffImagePlugin.py in load, line 382
Python Executable:  /usr/local/bin/python
Python Version: 2.7.1

我在Google上进行了搜索,但找不到任何相关答案.有人可以帮我发生什么事,我该如何解决?谢谢.

I searched on google but could not find any relevant answers. Could someone please help me what happened and how i can fix it? Thank You.

完全追溯

文件 "/lib/python2.7/django/core/handlers/base.py", 第111行,在get_response response = callback(request, * callback_args,** callback_kwargs)

File "/lib/python2.7/django/core/handlers/base.py", line 111, in get_response response = callback(request, *callback_args, **callback_kwargs)

文件 "/home/swaroop/project/apps/photography/views.py", showPhoto context_instance = RequestContext(request))中的第702行

File "/home/swaroop/project/apps/photography/views.py", line 702, in showPhoto context_instance=RequestContext(request))

文件 "/lib/python2.7/django/shortcuts/init.py", 第20行,在render_to_response返回中 HttpResponse(loader.render_to_string(* args,** kwargs), ** httpresponse_kwargs)

File "/lib/python2.7/django/shortcuts/init.py", line 20, in render_to_response return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

文件 "/lib/python2.7/django/template/loader.py", 第188行,在render_to_string中返回t.render(context_instance)

File "/lib/python2.7/django/template/loader.py", line 188, in render_to_string return t.render(context_instance)

文件 "/lib/python2.7/django/template/base.py", 123行,在渲染中返回self._render(context)

File "/lib/python2.7/django/template/base.py", line 123, in render return self._render(context)

文件 "/lib/python2.7/django/template/base.py", 第117行,在_render中返回self.nodelist.render(context)

File "/lib/python2.7/django/template/base.py", line 117, in _render return self.nodelist.render(context)

文件 "/lib/python2.7/django/template/base.py", 744行,在渲染bits.append(self.render_node(node,context))

File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))

文件 "/lib/python2.7/django/template/base.py", 757行,在render_node中返回node.render(context)

File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)

文件 "/lib/python2.7/django/template/loader_tags.py", 第127行,在渲染中返回Compiled_pa​​rent._render(context)

File "/lib/python2.7/django/template/loader_tags.py", line 127, in render return compiled_parent._render(context)

文件 "/lib/python2.7/django/template/base.py", 第117行,在_render中返回self.nodelist.render(context)

File "/lib/python2.7/django/template/base.py", line 117, in _render return self.nodelist.render(context)

文件 "/lib/python2.7/django/template/base.py", 744行,在渲染bits.append(self.render_node(node,context))

File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))

文件 "/lib/python2.7/django/template/base.py", 757行,在render_node中返回node.render(context)

File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)

文件 "/lib/python2.7/django/template/loader_tags.py", 第64行,渲染结果= block.nodelist.render(context)

File "/lib/python2.7/django/template/loader_tags.py", line 64, in render result = block.nodelist.render(context)

文件 "/lib/python2.7/django/template/base.py", 744行,在渲染bits.append(self.render_node(node,context))

File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))

文件 "/lib/python2.7/django/template/base.py", 757行,在render_node中返回node.render(context)

File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)

文件 "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", 第45行,在渲染中返回self._render(context)

File "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", line 45, in render return self._render(context)

文件 "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", 第97行,在渲染文件中,几何图形,**选项

File "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", line 97, in render file, geometry, **options

文件"/lib/python2.7/sorl/thumbnail/base.py",第61行, 在get_thumbnail缩略图中)

File "/lib/python2.7/sorl/thumbnail/base.py", line 61, in get_thumbnail thumbnail)

文件"/lib/python2.7/sorl/thumbnail/base.py",第86行, 在_create_thumbnail图片= default.engine.create(source_image, 几何形状,选项)

File "/lib/python2.7/sorl/thumbnail/base.py", line 86, in _create_thumbnail image = default.engine.create(source_image, geometry, options)

文件"/lib/python2.7/sorl/thumbnail/engines/base.py", 第15行,在create image = self.orientation(image,geometry, 选项)

File "/lib/python2.7/sorl/thumbnail/engines/base.py", line 15, in create image = self.orientation(image, geometry, options)

文件"/lib/python2.7/sorl/thumbnail/engines/base.py", 第26行,方向返回self._orientation(image)

File "/lib/python2.7/sorl/thumbnail/engines/base.py", line 26, in orientation return self._orientation(image)

文件 "/lib/python2.7/sorl/thumbnail/engines/pil_engine.py", 第29行,在_orientation exif = image._getexif()

File "/lib/python2.7/sorl/thumbnail/engines/pil_engine.py", line 29, in _orientation exif = image._getexif()

文件"/usr/local/lib/python2.7/site-packages/PIL/JpegImagePlugin.py", 381行,位于_getexif info.load(file)

File "/usr/local/lib/python2.7/site-packages/PIL/JpegImagePlugin.py", line 381, in _getexif info.load(file)

文件"/usr/local/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", 第382行,在负载升高IOError中,数据不足"

File "/usr/local/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 382, in load raise IOError, "not enough data"

IOError:数据不足

IOError: not enough data

推荐答案

更新

image._getexif被认为是高度实验性的.引用 sorl-thumbnail 和,您可以将代码更新为

image._getexif is claimed to be highly experimental. Refs sorl-thumbnail and issue #98, you could update the code to be

def _orientation(self, image):
    try:
        exif = image._getexif()
    except (AttributeError, IOError):
        exif = None


这是由PIL尝试加载损坏的或可能不受支持的TIFF文件引起的.
通常,当您使用forms.ImageField时,Django会检查上传图像的正确性.
因此,您需要:


It's caused by PIL's attempt to load a corrupted or possibly unsupported TIFF file.
Normally when you use forms.ImageField, Django would check the correctness of uploaded images.
Thus you need to:

  • 确保您正在使用forms.ImageField(这是models.ImageField的默认设置)来处理视图中的上载/上载
  • 通过

  • ensure that you're using forms.ImageField, which is default for models.ImageField, to deal w/ uploading in your view
  • check the uploaded image by

from PIL import Image
Image.open(path).load()

  • 使用处理TIFF的工具包打开图像,如果可以打开,将其保存到Jpeg或Png并更新模型实例的字段以指向新文件.
  • 您还可以限制用户上传jpeg/png/gif之类的常规格式,而不是TIFF

    Also you could limit user to upload normal formats such as jpeg/png/gif instead of TIFF

    这篇关于Python图像库错误-渲染时捕获IOError:数据不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-31 04:47