Django / VersatileImageField:IOError - 无法识别图像文件

时间:2015-04-19 01:09:04

标签: python django thumbnails

我是Python / Django的菜鸟。我继承了别人的应用程序。该应用程序具有此模型:

# -*- encoding: utf8 -*-
from django.db import models
from versatileimagefield.fields import VersatileImageField, PPOIField

class Motion(models.Model):
    name = models.CharField(max_length=100)

    cover = VersatileImageField(upload_to='motions/capa', ppoi_field='ppoi', null=True)
    ppoi = PPOIField()

    authors = models.ManyToManyField(Author)
    genre = models.ForeignKey(Genre, related_name='motions')
    description = models.TextField(verbose_name='Descrição')

    @property
    def cover_image(self):
        if self.cover:
            return self.cover.crop['150x150'].url

    def __unicode__(self):
        return self.name

在django的管理员中,我可以毫无问题地创建此模型的新对象,但是当我尝试浏览此模型的网址(/admin/motion/motion/1/)进行编辑时,我收到了这条消息:

IOError at /admin/motion/motion/1/

cannot identify image file <File: C:\Users\Leda\Projects\TreeOfBooks\media\motions\capa\Infinite_loop_BSOD.jpg>

回溯:

Environment:


Request Method: GET
Request URL: http://doispes:90/admin/motion/motion/1/

Django Version: 1.7.1
Python Version: 2.7.9
Installed Applications:
('grappelli',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'versatileimagefield',
 'tastypie',
 'motion',
 'loja',
 'gerente',
 'motiongen')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Template error:
In template C:\Python27\lib\site-packages\grappelli\templates\admin\includes\fieldset.html, error at line 24
   cannot identify image file <File: C:\Users\Leda\Projects\TreeOfBooks\media\motions\capa\Infinite_loop_BSOD.jpg>
   14 :                         {% if field.is_checkbox %}
   15 :                             <div class="c-1">&nbsp;</div>
   16 :                             <div class="c-2">
   17 :                                 {{ field.field }}{{ field.label_tag|prettylabel }}
   18 :                         {% else %}
   19 :                             <div class="c-1">{{ field.label_tag|prettylabel }}</div>
   20 :                             <div class="c-2">
   21 :                                 {% if field.is_readonly %}
   22 :                                     <div class="grp-readonly">{{ field.contents|linebreaksbr }}</div>
   23 :                                 {% else %}
   24 :                                      {{ field.field }} 
   25 :                                 {% endif %}
   26 :                         {% endif %}
   27 :                             {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
   28 :                             {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.field.errors }}{% endif %}
   29 :                             {% if field.field.help_text %}
   30 :                                 <p class="grp-help">{{ field.field.help_text|safe }}</p>
   31 :                             {% endif %}
   32 :                         </div>
   33 :                     </div>
   34 :                 {% endfor %}

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  137.                 response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py" in render
  103.             self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django\template\response.py" in rendered_content
  80.         content = template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  148.             return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
  142.         return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
  142.         return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
  142.         return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
  150.                 return template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  148.             return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
  142.         return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  402.         return strip_spaces_between_tags(self.nodelist.render(context).strip())
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  312.                 return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  312.                 return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  844.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  80.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render
  93.             output = force_text(output)
File "C:\Python27\lib\site-packages\django\utils\encoding.py" in force_text
  85.                 s = six.text_type(s)
File "C:\Python27\lib\site-packages\django\forms\forms.py" in __str__
  508.         return self.as_widget()
File "C:\Python27\lib\site-packages\django\forms\forms.py" in as_widget
  560.         return force_text(widget.render(name, self.value(), attrs=attrs))
File "C:\Python27\lib\site-packages\versatileimagefield\widgets.py" in render
  198.             attrs
File "C:\Python27\lib\site-packages\django\forms\widgets.py" in render
  815.             output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
File "C:\Python27\lib\site-packages\versatileimagefield\widgets.py" in render
  123.                     value
File "C:\Python27\lib\site-packages\versatileimagefield\widgets.py" in image_preview
  89.             'sized_url': value.thumbnail['300x300'],
File "C:\Python27\lib\site-packages\versatileimagefield\datastructures\sizedimage.py" in __getitem__
  111.                             height=height
File "C:\Python27\lib\site-packages\versatileimagefield\datastructures\sizedimage.py" in create_resized_image
  172.             path_to_image
File "C:\Python27\lib\site-packages\versatileimagefield\datastructures\base.py" in retrieve_image
  124.             Image.open(image),
File "C:\Python27\lib\site-packages\PIL\Image.py" in open
  2288.                   % (filename if filename else fp))

Exception Type: IOError at /admin/motion/motion/1/
Exception Value: cannot identify image file <File: C:\Users\Leda\Projects\TreeOfBooks\media\motions\capa\Infinite_loop_BSOD.jpg>

最重要的是,应用程序还使用django-tastypie通过REST api公开模型。当我浏览这个模型的REST URL(/api/v1/estante/?format=json)时,我得到了这个JSON响应:

{
    "error_message":"cannot identify image file &lt;File: C:\\Users\\Leda\\Projects\\TreeOfBooks\\media\\motions\\capa\\Infinite_loop_BSOD.jpg&gt;",
    "traceback":"Traceback (most recent call last):\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 201, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 432, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 464, in dispatch\n    response = method(request, **kwargs)\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1297, in get_list\n    bundles.append(self.full_dehydrate(bundle, for_list=True))\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 852, in full_dehydrate\n    bundle.data[field_name] = field_object.dehydrate(bundle, for_list=for_list)\n\n  File \"C:\\Python27\\lib\\site-packages\\tastypie\\fields.py\", line 116, in dehydrate\n    current_object = getattr(current_object, attr, None)\n\n  File \"C:\\Users\\Leda\\Projects\\TreeOfBooks\\motion\\models.py\", line 37, in img_capa\n    return self.capa.crop['150x150'].url\n\n  File \"C:\\Python27\\lib\\site-packages\\versatileimagefield\\datastructures\\sizedimage.py\", line 111, in __getitem__\n    height=height\n\n  File \"C:\\Python27\\lib\\site-packages\\versatileimagefield\\datastructures\\sizedimage.py\", line 172, in create_resized_image\n    path_to_image\n\n  File \"C:\\Python27\\lib\\site-packages\\versatileimagefield\\datastructures\\base.py\", line 124, in retrieve_image\n    Image.open(image),\n\n  File \"C:\\Python27\\lib\\site-packages\\PIL\\Image.py\", line 2288, in open\n    % (filename if filename else fp))\n\nIOError: cannot identify image file <File: C:\\Users\\Leda\\Projects\\TreeOfBooks\\media\\motions\\capa\\Infinite_loop_BSOD.jpg>\n"
}

如果我将行return self.capa.crop['150x150'].url更改为return self.capa,则JSON输出将更改为:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [
        {
            "authors": [
                {
                    "biography": "Meh",
                    "id": 1,
                    "name": "Meh",
                    "resource_uri": ""
                }
            ],
            "cover": "/media/motions/capa/Infinite_loop_BSOD.jpg",
            "chapters": [
                {
                    "file": "/media/motions/rsz_1rsz_11img_337901552885900.jpg",
                    "id": 1,
                    "number": 1,
                    "resource_uri": "",
                    "synopsis": ""
                },
                {
                    "file": "/media/motions/tumblr_na3jdpPTi71rpu8e5o1_1280.png",
                    "id": 2,
                    "number": 2,
                    "resource_uri": "",
                    "synopsis": ""
                }
            ],
            "description": "Meh",
            "datasheet": [
                {
                    "function": "AA",
                    "id": 1,
                    "name": "AA",
                    "resource_uri": "/api/v1/datasheet/1/"
                },
                {
                    "function": "BBB",
                    "id": 2,
                    "name": "BBB",
                    "resource_uri": "/api/v1/datasheet/2/"
                }
            ],
            "genre": {
                "id": 1,
                "name": "Cuidado hein",
                "resource_uri": "/api/v1/genre/1/"
            },
            "id": 1,
            "cover_image": "motions/capa/Infinite_loop_BSOD.jpg",
            "name": "Meh",
            "resource_uri": "/api/v1/estante/1/"
        }
    ]
}

但管理员的错误仍将存在。我怎么解决这个问题?我完全迷失了。

1 个答案:

答案 0 :(得分:0)

尝试https://stackoverflow.com/a/20863145/3502612的解决方案 另外一定要查看有关问题的评论。