Python图像库错误 - 渲染时捕获IOError:数据不足

swa*_*oop 0 python django sorl-thumbnail python-imaging-library

我创建了一个使用sorl-thumbnail来调整上传图像大小的网站.大多数图像都没有任何问题调整大小,但很少有人收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我在谷歌搜索但找不到任何相关的答案.有人可以帮我解决发生的事情以及如何解决这个问题?谢谢.

编辑

完成回溯

Traceback(最近一次调用最后一次):

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

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

文件"/lib/python2.7/django/shortcuts/ 初始化的.py",第20行,选择render_to_response中返回的HttpResponse(loader.render_to_string(*ARGS,**kwargs),**httpresponse_kwargs)

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

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

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

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

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

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

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

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

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

文件"/lib/python2.7/django/template/loader_tags.py",第64行,在render result = block.nodelist.render(context)中

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

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

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

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

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

文件"/lib/python2.7/sorl/thumbnail/base.py",第86行,在_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,options)中

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

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

在_getexif info.load(file)中输入文件"/usr/local/lib/python2.7/site-packages/PIL/JpegImagePlugin.py",第381行

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

IOError:没有足够的数据

okm*_*okm 6

更新

image._getexif据称是高度实验性的.参考sorl-thumbnail问题#98,您可以更新代码

def _orientation(self, image):
    try:
        exif = image._getexif()
    except (AttributeError, IOError):
        exif = None
Run Code Online (Sandbox Code Playgroud)

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

  • 确保您使用forms.ImageField(默认情况下)models.ImageField在视图中进行上传处理
  • 检查上传的图像

    from PIL import Image
    Image.open(path).load()
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用处理TIFF的工具包打开图像,如果可以打开它,将其保存到Jpeg或Png并更新模型实例的字段以指向新文件.

此外,您可以限制用户上传普通格式,如jpeg/png/gif而不是TIFF