使用新的 sorl-thumbnail 进行图像过滤

发布于 2024-11-09 22:32:23 字数 221 浏览 4 评论 0原文

我正在尝试将一些旧网站升级到最新版本的 Django,并且 sorl-thumbnail 也需要更新。

我已将一些模板修复为新的 {%thumbnail ... %} {% endthumbnail %} 格式,但我在使用内置和自定义过滤器(或处理器)时遇到问题。我有一个用于将缩略图设为黑色和黑色的。白色和自定义书写的用于将饱和度设置为 50%。如何使用最新版本的 sorl-thumbnail 来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-11-16 22:32:25

看来新的 sorl 代码库的功能已经消失了。

但是,您可以通过创建(通过子类化)引擎、设置 THUMBNAIL_ENGINE 并覆盖 create() 方法来实现自定义处理。

例如,要添加处理选项来生成圆角:

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

,您可以在模板中将其调用为(请注意 cornerradius 选项):

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

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 %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文