Modify image before it's handled by stdimage

30 views
Skip to first unread message

Mark

unread,
Sep 25, 2015, 4:58:16 PM9/25/15
to Django users

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

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(
                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).

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



Reply all
Reply to author
Forward
0 new messages