Resizable animated image

Is it possible to resize animated gifs using sorl?

+5
source share
4 answers

Wow, this is a special request that I never expected! sorl.thumbnailnow tuned to the engine and comes with PIL and pgmagick. I think there are ways to make imagemagick resize animated gifs, and maybe pgmagick can do it, but I have not tested this, and it is very unlikely to work with the shipped engines as they are.

+6
source

I managed to get sorl to work with gif.

  • (PIL gif - ). pgmagick, , .
  • - :

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

sorl-thumnail, , .

UPD: . , , .

+2

I have a working solution (verified with scol-thumbnail 11.12.1b). Wand backend required:

#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
+2
source

I will talk about 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
0
source

All Articles