Image filtering with the new sorl-thumbnail

二次信任 提交于 2019-12-21 06:37:38

问题


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?


回答1:


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 %}


来源:https://stackoverflow.com/questions/6151468/image-filtering-with-the-new-sorl-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!