jazzband / sorl-thumbnail

Thumbnails for Django
https://sorl-thumbnail.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.74k stars 493 forks source link

TypeError in pil_engine. #322

Open narrowfail opened 9 years ago

narrowfail commented 9 years ago

Got the following Traceback while using sorl-thumbnail 11.12.1b in production using Pillow.

Internal Server Error: /core/company/view/2447/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "/srv/mysite/mysite/mysite/core/views.py", line 385, in get
    context_instance=RequestContext(request))
  File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py", line 23, in render_to_response
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 178, in render_to_string
    return t.render(context_instance)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 148, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 142, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 126, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 142, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/templatetags/thumbnail.py", line 58, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/templatetags/thumbnail.py", line 136, in _render
    thumbnail = get_thumbnail(file_, geometry, **options)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/shortcuts.py", line 8, in get_thumbnail
    return default.backend.get_thumbnail(file_, geometry_string, **options)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/base.py", line 118, in get_thumbnail
    thumbnail)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/base.py", line 151, in _create_thumbnail
    default.engine.write(image, options, thumbnail)
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/engines/base.py", line 142, in write
    progressive=progressive
  File "/usr/local/lib/python2.7/dist-packages/sorl/thumbnail/engines/pil_engine.py", line 223, in _get_raw_data
    image.save(bf, **params)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1663, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 600, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 459, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 428, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float
narrowfail commented 9 years ago

I did some debug and found this:

encoder = <built-in function jpeg_encoder>
args    = ('RGB',)
mode = 'RGB'
encoder_name = 'jpeg'
extra = (95, True, 0, True, 0, 0.0, 0.0, -1, None, '', '')
narrowfail commented 9 years ago

The problem was:

File Type: PC bitmap, Windows 3.x format, 285 x 78 x 24 MIME Type: image/x-ms-bmp Suggested file extension(s): bmp

The file is a BMP, not a JPG.

Is there any way to avoid crashin the site just because an image has a bad extension? Thanks!

relekang commented 9 years ago

Thank you for reporting this. I'll look into it. What is THUMBNAIL_PRESERVE_FORMAT set to?

narrowfail commented 9 years ago

THUMBNAIL_PRESERVE_FORMAT is set to the default value because I didn't change it.

relekang commented 9 years ago

Can you try to set it to True?

narrowfail commented 9 years ago

Same error: Exception Type: TypeError Exception Value: integer argument expected, got float This is the image that generates the error: http://www.msnj.org/media/nvftnkjx.jpg

relekang commented 9 years ago

Thank you for checking, I look into this as soon as possible

sbaechler commented 9 years ago

Sorl (Pillow) also crashes if you upload a TIFF but don't have it compiled with tiff support. (libtiff missing)