django-avatar:无法保存缩略图

时间:2014-06-07 13:11:44

标签: python django

我使用django-avatar应用,无法保存缩略图。原始图像通常保存在我的媒体目录中。 使用步骤执行显示此处发生错误

image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)

我在create_thumbnail中找到了这一行:

def create_thumbnail(self, size, quality=None):
    # invalidate the cache of the thumbnail with the given size first
    invalidate_cache(self.user, size)
    try:
        orig = self.avatar.storage.open(self.avatar.name, 'rb')
        image = Image.open(orig)
        quality = quality or settings.AVATAR_THUMB_QUALITY
        w, h = image.size
        if w != size or h != size:
            if w > h:
                diff = int((w - h) / 2)
                image = image.crop((diff, 0, w - diff, h))
            else:
                diff = int((h - w) / 2)
                image = image.crop((0, diff, w, h - diff))
            if image.mode != "RGB":
                image = image.convert("RGB")
            image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD)
            thumb = six.BytesIO()
            image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)
            thumb_file = ContentFile(thumb.getvalue())
        else:
            thumb_file = File(orig)
        thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
    except IOError:
        return  # What should we do here?  Render a "sorry, didn't work" img?
也许我需要的只是一些图书馆? 谢谢

1 个答案:

答案 0 :(得分:1)

错误是:'编码器jpeg不可用'。 我已经解决了这个问题:

sudo apt-get build-dep python-imaging

pip uninstall Pillow
pip install Pillow