TypeError使用sorl-thumbnail和Pillow

时间:2014-11-05 03:05:39

标签: python django pillow sorl-thumbnail

即时为Django使用sorl-thumbnail,我发现了一个错误:

Internal Server Error: /history/company/
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 "/srv/mysite/mysite/mysite/history/views.py", line 39, in company_history_view
    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/django/template/defaulttags.py", line 201, in render
    nodelist.append(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 1685, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 696, 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 473, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 430, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float

我不知道这是一个sorl-thumbnail错误还是PILLOW错误。 我也不知道哪个参数失败了。我怎么能搞清楚这一点? 我应该开始调试吗?

谢谢!

编辑1:

开始使用debbugin,最后一个参数是:

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, '', '')

编辑2: 在/usr/local/lib/python2.7/dist-packages/PIL/Image.py中保存在第1685行: save_handler(self,fp,filename) - 参数是:

fp = <cStringIO.StringO object at 0x7f0d4f3ae688>
format = 'JPEG'
self = <PIL.Image.Image image mode=RGB size=75x21 at 0x7F0D4F2079E0>
filename = ''
ext = ''
params = {'compression': 0, 'dpi': (0.0, 0.0),'optimize': 1,  'progressive': True, 'quality': 95}
close = 0
save_handler = <function _save at 0x7f0d4f13d230>

他们看起来很正常,也许是PILLOW的错误。

1 个答案:

答案 0 :(得分:0)

找到原因:

文件类型:PC位图,Windows 3.x格式,285 x 78 x 24 MIME类型:image / x-ms-bmp 建议的文件扩展名:bmp

该文件是BMP,而不是JPG。

对不起。