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

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


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


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(
        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:

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


So I created my custom field, to catch the image before it's passed to 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):

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)?


1 个解决方案



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


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:


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 as f:
        with 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:
      , file_format)
                f = ContentFile(file_buffer.getvalue())
                # delete the original big image
                # save the resized version with the same filename and format
      , 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:


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

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


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)。



粤ICP备14056181号  © 2014-2020