Django在被stdimage处理之前修改图像

[英]Django modify image before it's handled by stdimage


I'm using django-stdimage for creating variations of the image.

我使用django-stdimage创建图像的变化。

class Photo(models.Model):
    photo = StdImageField(upload_to='photos', verbose_name=_("photo"),
                      variations={'large': (600, 600), 'thumbnail': (100, 100)}

StdImageField does it's own operations on the image, subclassing ImageField and having attr_class = StdImageFieldFile

StdImageField对图像进行自己的操作,子类化ImageField,并具有attr_class = StdImageFieldFile

StdImageFieldFile does the actual save operation

StdImageFieldFile执行实际的保存操作。

class StdImageFieldFile(ImageFieldFile):
    """
    Like ImageFieldFile but handles variations.
    """

    def save(self, name, content, save=True):
        super(StdImageFieldFile, self).save(name, content, save)
        render_variations = self.field.render_variations
        if callable(render_variations):
            render_variations = render_variations(
                file_name=self.name,
                variations=self.field.variations,
                storage=self.storage,
            )
        if not isinstance(render_variations, bool):
            msg = (
                '"render_variations" callable expects a boolean return value,'
                ' but got %s'
                ) % type(render_variations)
            raise TypeError(msg)
        if render_variations:
            self.render_variations()

However, I want to do some manipulation of the image before StdImageFieldFile does it (rotating).

但是,我想在StdImageFieldFile执行之前对图像进行一些操作(旋转)。

So I created my custom field, to catch the image before it's passed to stdimage

所以我创建了我的自定义字段,以便在图像传递给stdimage之前捕获图像

class Rotate(ImageFieldFile):
    def save(self, name, content, save=True):
        save = False

        return super(Rotate, self).save(name, content, save)

class StdImageFieldFileRotateMixin(Rotate, StdImageFieldFile):
    pass

class StdImageFieldRotate(StdImageField):
    attr_class = StdImageFieldFileRotateMixin

I have the image in the content property of the Rotate class and I can manipulate the image using PIL, but after it's done, I don't know how to assign this image back to the content property. It seems that it's generated on the lower level. Is there a method to generate this content property and then MRO will handle the rest (i.e. pass it to StdImageFieldFile and it will do the rest)?

我在Rotate类的content属性中有这个图像,我可以使用PIL来操作这个图像,但是完成之后,我不知道如何将这个图像分配回content属性。它似乎是在较低的水平上产生的。是否有一种方法来生成这个内容属性,然后MRO将处理其余的(例如,将它传递给StdImageFieldFile,它将处理其余的)?

1 个解决方案

#1


1  

You can pass a callable to the argument render_variations of the StdImageField class and pre-process the image there.

您可以将可调用性传递给StdImageField类的参数render_variation,并在那里对映像进行预处理。

For example assume you want to save disk space and resize the original image to a smaller version, taking only the smaller image together with variations created by django-stdimage. You can write a function like this:

例如,假设您希望节省磁盘空间,并将原始映像调整为更小的版本,只使用较小的映像和django-stdimage创建的变体。你可以这样写一个函数:

from io import BytesIO
from PIL import Image

from django.core.files.base import ContentFile

from stdimage.utils import render_variations

def preprocess(file_name, variations, storage):
    with storage.open(file_name) as f:
        with Image.open(f) as image:
            file_format = image.format

            # resize to a maximum of 1000x1000 keeping aspect ratio
            image.thumbnail((1000, 1000), resample=Image.ANTIALIAS)

            with BytesIO() as file_buffer:
                image.save(file_buffer, file_format)
                f = ContentFile(file_buffer.getvalue())
                # delete the original big image
                storage.delete(file_name)
                # save the resized version with the same filename and format
                storage.save(file_name, f)

    # render stdimage variations
    render_variations(file_name, variations, replace=True, storage=storage)

    return False  # prevent default rendering

You can then pass the function to the render_variations argument of the StdImageField class, like this:

然后,您可以将函数传递给StdImageField类的render_changes参数,如下所示:

class Photo(models.Model):
    photo = StdImageField(upload_to='photos', verbose_name=_("photo"),
                  variations={'large': (600, 600), 'thumbnail': (100, 100)},
                  render_variations=preprocess)

This technique is inspired from the Async image processing example in the README of the django-stdimage project on Github.

这种技术的灵感来自Github上的django-stdimage项目中的异步图像处理示例。

Also the way in which the image is handled in the preprocess function is inspired from the stdimage.models.StdImageFieldFile.render_variation method and uses Django Storage objects to deal with different storage types for image files (think Amazon S3).

在预处理函数中处理图像的方式也是从stdimage.model . stdimagefieldfile中获得的。render_change方法并使用Django存储对象来处理图像文件的不同存储类型(如Amazon S3)。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/09/25/7205d3cc783031ccbe127d54b7d35346.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告