Python Image Library Error - Caught IOError while rendering: not enough data

 ̄綄美尐妖づ 提交于 2020-01-06 15:51:26

问题


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

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.

EDIT

Complete Traceback

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


回答1:


update

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

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:

  • 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()
    
  • use toolkit that processing TIFF to open the image and, if you can open it , save it to Jpeg or Png and update the field of the model instance to point to the new file.

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



来源:https://stackoverflow.com/questions/10583265/python-image-library-error-caught-ioerror-while-rendering-not-enough-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!