Thumbnails for Django.
- Support for Django 5.2, 6.0, and 6.1 following the Django supported versions policy
- Storage support
- Pluggable Engine support for Pillow, ImageMagick, PIL, Wand, pgmagick, and vipsthumbnail
- Pluggable Key Value Store support (cached db, redis, and dynamodb by AWS)
- Pluggable Backend support
- Admin integration with possibility to delete
- Dummy generation (placeholders)
- Flexible, simple syntax, generates no html
- CSS style cropping options
- Back smart cropping, and remove borders from the images when cropping
- Margin calculation for vertical positioning
- Alternative resolutions versions of a thumbnail
Read more in the documentation (latest version)
By contributing you agree to abide by the Code of Conduct. To report a security issue, follow the security policy instead of opening a public issue.
Feel free to create a new Pull request if you want to propose a new feature. If you need development support or want to discuss with other developers join us in GitHub Discussions.
The tests should run with tox. Running tox will run all tests for all environments. However, it is possible to run a certain environment with tox -e <env>, a list of all environments can be found with tox -l. These tests require the dependencies of the different engines defined in the documentation. The easiest way to run tests is using Docker - see the contributing documentation for detailed instructions on running tests locally with Docker.
If you need help using sorl-thumbnail ask in the Q&A category of GitHub Discussions.
Getting the code for the latest stable release use 'pip'.
$ pip install sorl-thumbnail
Then register 'sorl.thumbnail', in the 'INSTALLED_APPS' section of your project's settings.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sorl.thumbnail',
]
All of the examples assume that you first load the thumbnail template tag in your template.:
{% load thumbnail %}
A simple usage.
{% thumbnail item.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
See more examples in the section Template examples in the Documentation
You can use sorl.thumbnail.ImageField in your models:
from django.db import models
from sorl.thumbnail import ImageField
class Item(models.Model):
image = ImageField(upload_to='whatever')
See more examples in the section Model examples in the Documentation
You can use the 'get_thumbnail':
from sorl.thumbnail import get_thumbnail from sorl.thumbnail import delete im = get_thumbnail(my_file, '100x100', crop='center', quality=99) delete(my_file)
See more examples in the section Low level API examples in the Documentation
Alternatively, you load the templatetags by {% load sorl_thumbnail %}
instead of traditional {% load thumbnail %}. It's especially useful in
projects that do make use of multiple thumbnailer libraries that use the
same name (thumbnail) for the templatetag module:
{% load sorl_thumbnail %}
{% thumbnail item.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
Possible related to the implementation of your Amazon S3 Backend, see the issue #351 due the storage backend reviews if there is an existing thumbnail when tries to generate the thumbnail that makes an extensive use of the S3 API
A fast workaround if you are not willing to tweak your storage backend is to set:
THUMBNAIL_FORCE_OVERWRITE = True
So it will avoid to overly query the S3 API.