Easy Thumbnails for Django
Easy Thumbnails is a powerful yet user-friendly Django application designed for generating and managing image thumbnails. It dynamically creates thumbnails based on a source image, supports predefined aliases, and integrates seamlessly with Django models and templates. The library is actively maintained, with the current stable version being 2.10.1, and releases occurring regularly to support new Django and Python versions.
Warnings
- breaking Version 2.10.0 dropped support for Python 3.8 and Django 4.1 and earlier. Ensure your project meets the new minimum requirements.
- breaking Version 2.8.0 removed the `THUMBNAIL_HIGH_RESOLUTION` and `THUMBNAIL_HIGHRES_INFIX` settings. High-resolution support is now handled differently, if still needed, consult the latest documentation for alternatives.
- gotcha If you use multiple thumbnailing libraries (e.g., `sorl-thumbnail` and `easy-thumbnails`) in the same project, the default `{% load thumbnail %}` template tag might clash.
- gotcha When using remote storage backends (e.g., S3), fetching image dimensions can be slow. This can impact performance for pages rendering many thumbnails.
- deprecated Version 2.8.4 replaced deprecated Pillow constants against newer counterparts. While this is an internal change, older custom processors or direct Pillow interactions might need adjustment.
Install
-
pip install easy-thumbnails
Imports
- ThumbnailerImageField
from easy_thumbnails.fields import ThumbnailerImageField
- get_thumbnailer
from easy_thumbnails.files import get_thumbnailer
- template_tag_load
{% load thumbnail %}
Quickstart
import os
# settings.py
INSTALLED_APPS = [
# ...
'easy_thumbnails',
]
THUMBNAIL_ALIASES = {
'': {
'avatar': {'size': (50, 50), 'crop': True},
'profile_square': {'size': (200, 200), 'crop': 'smart'},
},
}
# models.py
from django.db import models
from easy_thumbnails.fields import ThumbnailerImageField
class Profile(models.Model):
user = models.OneToOneField('auth.User', on_delete=models.CASCADE)
photo = ThumbnailerImageField(upload_to='profile_photos', blank=True, null=True)
def __str__(self):
return self.user.username
# In a Django template (e.g., profile.html)
# {% load thumbnail %}
# <img src="{{ profile.photo|thumbnail_url:'avatar' }}" alt="{{ profile.user.username }} avatar" />
# Or, with custom options directly in template
# {% load thumbnail %}
# <img src="{% thumbnail profile.photo 100x100 crop %}" alt="Profile photo" />
# In Python code
# from easy_thumbnails.files import get_thumbnailer
# profile_instance = Profile.objects.get(pk=1)
# if profile_instance.photo:
# thumbnailer = get_thumbnailer(profile_instance.photo)
# avatar_url = thumbnailer['avatar'].url
# print(f"Avatar URL: {avatar_url}")
# Run migrations after adding to INSTALLED_APPS and defining models:
# python manage.py migrate easy_thumbnails