使用 sorl-thumbnail 调整大小的动画 gif

发布于 2024-10-12 05:58:44 字数 32 浏览 5 评论 0原文

有没有可能用 sorl 调整 GIF 动画的大小?

Is there any possibility to resize animated gifs with sorl?

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

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

发布评论

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

评论(4

梦太阳 2024-10-19 05:58:44

哇,这是我从未想到的功能请求! sorl.thumbnail 现在是引擎可配置的,并附带 PIL 和 pgmagick。我认为有一些方法可以让 imagemagick 调整动画 gif 的大小,所以也许 pgmagick 可以做到这一点,但我还没有测试过这一点,而且它不太可能按原样与附带的引擎一起工作。

Wow, thats a feature request I would never expect! sorl.thumbnail is now engine configurable and comes with PIL and pgmagick. I think there are ways to make imagemagick resize animated gifs and so maybe pgmagick can do this but I have not tested this and its very unlikely to work with the shipped engines as is.

要走就滚别墨迹 2024-10-19 05:58:44

我有工作解决方案(使用 sorl-thumbnail 11.12.1b 进行测试)。需要 Wand 后端:

#sorl_extensions.py
from sorl.thumbnail.base import (
    ThumbnailBackend, EXTENSIONS,
    default_settings as thumbnail_default_settings
)


EXTENSIONS.update({'GIF': 'gif'})


class GifThumbnailBackend(ThumbnailBackend):
    def _get_format(self, file_):
        file_extension = self.file_extension(file_)

        if file_extension == '.jpg' or file_extension == '.jpeg':
            return 'JPEG'
        elif file_extension == '.png':
            return 'PNG'
        elif file_extension == '.gif':
            return 'GIF'
        else:
            from django.conf import settings

            return getattr(settings, 'THUMBNAIL_FORMAT', thumbnail_default_settings.THUMBNAIL_FORMAT)

#settings.py
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.wand_engine.Engine'
THUMBNAIL_BACKEND = 'tools.sorl_extensions.GifThumbnailBackend'
THUMBNAIL_PRESERVE_FORMAT = True

I've got working solution (tested with sorl-thumbnail 11.12.1b). Requires Wand backend:

#sorl_extensions.py
from sorl.thumbnail.base import (
    ThumbnailBackend, EXTENSIONS,
    default_settings as thumbnail_default_settings
)


EXTENSIONS.update({'GIF': 'gif'})


class GifThumbnailBackend(ThumbnailBackend):
    def _get_format(self, file_):
        file_extension = self.file_extension(file_)

        if file_extension == '.jpg' or file_extension == '.jpeg':
            return 'JPEG'
        elif file_extension == '.png':
            return 'PNG'
        elif file_extension == '.gif':
            return 'GIF'
        else:
            from django.conf import settings

            return getattr(settings, 'THUMBNAIL_FORMAT', thumbnail_default_settings.THUMBNAIL_FORMAT)

#settings.py
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.wand_engine.Engine'
THUMBNAIL_BACKEND = 'tools.sorl_extensions.GifThumbnailBackend'
THUMBNAIL_PRESERVE_FORMAT = True
无戏配角 2024-10-19 05:58:44

我设法让 sorl 与 gif 一起工作。

  1. 您需要使用图像 magick 或图形 magick 后端(PIL 不支持 gif 大小调整,无需进行一些黑客攻击)。你也可以尝试pgmagick,但我不知道它是否有效。
  2. 将它们放在代码中的某个位置:

    来自 sorl.thumbnail 导入库
    base.EXTENSIONS.update({'GIF': 'gif'})
    

您可以查看 sorl-thumnail 源代码以了解其工作原理

UPD:未经测试。仅当您确定自己在做什么时才使用此选项。

I managed to make sorl work with gif.

  1. You need to use image magick or graphics magick backend (PIL does not support gif resizing without some hacking). You can also try pgmagick, but I don't know if it will work.
  2. Put these somewhere in your code:

    from sorl.thumbnail import base
    base.EXTENSIONS.update({'GIF': 'gif'})
    

You can take a look at sorl-thumnail source to find how this works

UPD: It is untested. Use this only if you are sure about what you are doing.

漫漫岁月 2024-10-19 05:58:44

我将分享我的例子:

# -*- coding: utf-8 -*-
import os
from sorl.thumbnail import get_thumbnail

def get_file_extension(obj):
    filename, file_extension = os.path.splitext(obj)
    return file_extension

def get_thumbnail_size(obj, size):
    img_format = 'JPEG'
    if get_file_extension(obj.url) == '.png':
        img_format = 'PNG'
    if get_file_extension(obj.url) == '.gif':
        from sorl.thumbnail import base
        base.EXTENSIONS.update({'GIF': 'gif'})
        img_format = 'GIF'
    return get_thumbnail(obj, size, quality=90, format=img_format).url

I will share my example:

# -*- coding: utf-8 -*-
import os
from sorl.thumbnail import get_thumbnail

def get_file_extension(obj):
    filename, file_extension = os.path.splitext(obj)
    return file_extension

def get_thumbnail_size(obj, size):
    img_format = 'JPEG'
    if get_file_extension(obj.url) == '.png':
        img_format = 'PNG'
    if get_file_extension(obj.url) == '.gif':
        from sorl.thumbnail import base
        base.EXTENSIONS.update({'GIF': 'gif'})
        img_format = 'GIF'
    return get_thumbnail(obj, size, quality=90, format=img_format).url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文