我创建了一个网站,它使用 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
我在谷歌上搜索但找不到任何相关答案。有人可以帮助我发生了什么事,我该如何解决?谢谢。
编辑
完整的回溯
Traceback (most recent call last):
File "/lib/python2.7/django/core/handlers/base.py", line 111, in get_response response = callback(request, *callback_args, **callback_kwargs)
File "/home/swaroop/project/apps/photography/views.py", line 702, in showPhoto context_instance=RequestContext(request))
File "/lib/python2.7/django/shortcuts/init.py", line 20, in render_to_response return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/lib/python2.7/django/template/loader.py", line 188, in render_to_string return t.render(context_instance)
File "/lib/python2.7/django/template/base.py", line 123, in render return self._render(context)
File "/lib/python2.7/django/template/base.py", line 117, in _render return self.nodelist.render(context)
File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))
File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)
File "/lib/python2.7/django/template/loader_tags.py", line 127, in render return compiled_parent._render(context)
File "/lib/python2.7/django/template/base.py", line 117, in _render return self.nodelist.render(context)
File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))
File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)
File "/lib/python2.7/django/template/loader_tags.py", line 64, in render result = block.nodelist.render(context)
File "/lib/python2.7/django/template/base.py", line 744, in render bits.append(self.render_node(node, context))
File "/lib/python2.7/django/template/base.py", line 757, in render_node return node.render(context)
File "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", line 45, in render return self._render(context)
File "/lib/python2.7/sorl/thumbnail/templatetags/thumbnail.py", line 97, in render file, geometry, **options
File "/lib/python2.7/sorl/thumbnail/base.py", line 61, in get_thumbnail thumbnail)
File "/lib/python2.7/sorl/thumbnail/base.py", line 86, in _create_thumbnail image = default.engine.create(source_image, geometry, options)
File "/lib/python2.7/sorl/thumbnail/engines/base.py", line 15, in create image = self.orientation(image, geometry, options)
File "/lib/python2.7/sorl/thumbnail/engines/base.py", line 26, in orientation return self._orientation(image)
File "/lib/python2.7/sorl/thumbnail/engines/pil_engine.py", line 29, in _orientation exif = image._getexif()
File "/usr/local/lib/python2.7/site-packages/PIL/JpegImagePlugin.py", line 381, in _getexif info.load(file)
File "/usr/local/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 382, in load raise IOError, "not enough data"
IOError: not enough data
最佳答案
更新
image._getexif
据称是高度实验性的。引用文献 sorl-thumbnail和 issue #98 ,您可以将代码更新为
def _orientation(self, image):
try:
exif = image._getexif()
except (AttributeError, IOError):
exif = None
这是由于 PIL 尝试加载损坏的或可能不受支持的 TIFF 文件引起的。
通常当你使用 forms.ImageField
时,Django 会检查上传图片的正确性。
因此你需要:
- 确保您正在使用
forms.ImageField
(models.ImageField
的默认设置)来处理 View 中的上传 通过
检查上传的图片from PIL import Image Image.open(path).load()
- 使用处理 TIFF 的工具包打开图像,如果可以打开,将其保存为 Jpeg 或 Png 并更新模型实例的字段以指向新文件。
您还可以限制用户上传普通格式,如 jpeg/png/gif 而不是 TIFF
关于Python 图像库错误 - 渲染 : not enough data 时捕获 IOError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583265/