开发者

Image filtering with the new sorl-thumbnail

开发者 https://www.devze.com 2023-03-08 15:21 出处:网络
I\'m trying to upgrade some older websites to the latest version of Django and sorl-thumbnail needs to be updated as well.

I'm trying to upgrade some older websites to the latest version of Django and sorl-thumbnail needs to be updated as well.

I have fixed some templates to the new {% thumbnail ... %} {% endthumbnail %} format but I'm having trouble with using both the built-in and custom filters (or processors). I had one for making a thumbnail black & white and a开发者_如何转开发 custom written one for setting saturation to 50%. How can I do that with the latest version of sorl-thumbnail?


It seems that functionality is gone with the new sorl codebase.

However, you can implement custom processing by creating (by subclassing) an engine, setting THUMBNAIL_ENGINE and overriding the create() method.

For example, to add a processing option to generate rounded corners:

from sorl.thumbnail.engines.pil_engine import Engine

class RoundedCornerEngine(Engine):    
    def create(self, image, geometry, options):
        image = super(RoundedCornerEngine, self).create(image, geometry, options)
        image = self.cornerize(image, geometry, options)
        return image

    def cornerize(self, image, geometry, options):
        if 'cornerradius' in options:
            ...whatever...
        return image

and you'd call that in a template as (note the cornerradius option):

{% thumbnail my_image "300x150" format="PNG" cornerradius=10 as thumb %}
    <img class="thumb" src="{{ thumb.url }}">
{% endthumbnail %}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号