Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automatic thumbnail format for Sorl - see
from sorl.thumbnail.conf import settings
from sorl.thumbnail.base import ThumbnailBackend
'png': 'PNG',
'jpeg': 'JPEG',
'jpg': 'JPEG',
class AutoFormatBackend(ThumbnailBackend):
def get_thumbnail(self, file_, geometry_string, **options):
Sets the format option (if not explicitly set) to the same format as the
original file.
if not options.get('format'):
ext = str(file_).split('.')[-1].lower()
options['format'] = FORMAT_DICT.get(ext, settings.THUMBNAIL_FORMAT)
return super(AutoFormatBackend, self).get_thumbnail(file_, geometry_string, **options)


Place on your path, and add the following to your django settings.

THUMBNAIL_BACKEND = 'sorl_backends.AutoFormatBackend'

# this is still used if the format cannot be automatically inferred (eg for a .gif image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment