如果max_width超过x与PIL,如何调整图像大小?

时间:2015-01-19 16:56:59

标签: python django python-imaging-library

我正在使用Django调整大小的库来自动调整上传的图像。我遇到的问题是,当图像调整大小时,尽管设置质量达到100,但图像质量仍然有所下降。我对PIL的了解不够充实,而且我对这个代码很不满意。 ;我不确定如何提高智商,所以我想做的是放入条件,如果图像宽度> 600然后调整图像大小(这样一定宽度内的图像不会调整大小并在智商中受到影响)。我的问题是,我应该将if条件放在ResizedImageField的构造函数中吗?或者我应该把它放在ResizedImageFileField中的save方法吗?

DJANGO模特

class MyModel(models.Model):
    ...
    image = ResizedImageField(max_width=500, max_height=300, upload_to='whatever')

DJANGO-RESIZED FORMS.PY

import os
try:
    from PIL import Image, ImageFile
except ImportError:
    import Image, ImageFile

try:
    # python3
    from io import BytesIO as StringIO
except ImportError:
    # python2
    from StringIO import StringIO

from django.conf import settings
from django.core.files.base import ContentFile

try:
    from sorl.thumbnail import ImageField
except ImportError:
    from django.db.models import ImageField


DEFAULT_SIZE = getattr(settings, 'DJANGORESIZED_DEFAULT_SIZE', [1920, 1080])
DEFAULT_COLOR = (255, 255, 255, 0)


class ResizedImageFieldFile(ImageField.attr_class):

    def save(self, name, content, save=True):
        new_content = StringIO()
        content.file.seek(0)
        thumb = Image.open(content.file)
        thumb.thumbnail((
            self.field.max_width,
            self.field.max_height
            ), Image.ANTIALIAS)

        if self.field.use_thumbnail_aspect_ratio:
            img = Image.new("RGBA", (self.field.max_width, self.field.max_height), self.field.background_color)
            img.paste(thumb, ((self.field.max_width - thumb.size[0]) / 2, (self.field.max_height - thumb.size[1]) / 2))
        else:
            img = thumb

        try:
            img.save(new_content, format=thumb.format, **img.info)
        except IOError:
            ImageFile.MAXBLOCK = img.size[0] * img.size[1]
            img.save(new_content, format=thumb.format, **img.info)

        new_content = ContentFile(new_content.getvalue())

        super(ResizedImageFieldFile, self).save(name, new_content, save)


class ResizedImageField(ImageField):

    attr_class = ResizedImageFieldFile

    def __init__(self, verbose_name=None, name=None, **kwargs):
        //if self.max_width > 400:
        self.max_width = kwargs.pop('max_width', DEFAULT_SIZE[0])
        self.max_height = kwargs.pop('max_height', DEFAULT_SIZE[1])
        self.use_thumbnail_aspect_ratio = kwargs.pop('use_thumbnail_aspect_ratio', False)
        self.background_color = kwargs.pop('background_color', DEFAULT_COLOR)
        super(ResizedImageField, self).__init__(verbose_name, name, **kwargs) 


try:
    from south.modelsinspector import add_introspection_rules
    rules = [
        (
            (ResizedImageField,),
            [],
            {
                "max_width": ["max_width", {'default': DEFAULT_SIZE[0]}],
                "max_height": ["max_height", {'default': DEFAULT_SIZE[1]}],
                "use_thumbnail_aspect_ratio": ["use_thumbnail_aspect_ratio", {'default': False}],
                "background_color": ["background_color", {'default': DEFAULT_COLOR}],
            },
        )
    ]
    add_introspection_rules(rules, ["^django_resized\.forms\.ResizedImageField"])
except ImportError:
    pass

2 个答案:

答案 0 :(得分:0)

似乎django-resized在保存时没有提供quality参数。对于除JPEG以外的其他图像格式,此参数将被忽略,因此您可以随时轻松地传递它。

如果使用枕头(PIL前叉),您可以使用quality='keep'使用与原始图像相同的量化表。否则,您可以将质量百分比明确指定为整数(不要使用100,它会创建非常大的图像)。

答案 1 :(得分:0)

它当然不漂亮。我意识到我有很多重复的代码,但这是我能弄清楚如何使其工作的唯一方法。

class ResizedImageFieldFile(ImageField.attr_class):

    def save(self, name, content, save=True): 
        new_content = StringIO()
        content.file.seek(0)
        thumb = Image.open(content.file)

        if thumb.size[0] > 900:
            thumb.thumbnail((
                self.field.max_width,
                self.field.max_height
                ), Image.ANTIALIAS)

            if self.field.use_thumbnail_aspect_ratio:
                img = Image.new("RGBA", (self.field.max_width, self.field.max_height), self.field.background_color)
                img.paste(thumb, ((self.field.max_width - thumb.size[0]) / 2, (self.field.max_height - thumb.size[1]) / 2))
            else:
                img = thumb

            try:
                img.save(new_content, format=thumb.format, **img.info)
            except IOError:
                ImageFile.MAXBLOCK = img.size[0] * img.size[1]
                img.save(new_content, format=thumb.format, **img.info)
        else:
            img = thumb
            try:
                img.save(new_content, format=thumb.format, **img.info)
            except IOError:
                ImageFile.MAXBLOCK = img.size[0] * img.size[1]
                img.save(new_content, format=thumb.format, **img.info)

        new_content = ContentFile(new_content.getvalue())

        super(ResizedImageFieldFile, self).save(name, new_content, save)