Easy Thumbnails for Django

2.10.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

After installing and adding 'easy_thumbnails' to `INSTALLED_APPS`, run migrations. Define thumbnail aliases in `THUMBNAIL_ALIASES` in your `settings.py` for reusable thumbnail configurations. Integrate `ThumbnailerImageField` into your models. In templates, load the `thumbnail` tags and use the `thumbnail_url` filter with an alias, or the `thumbnail` tag with explicit size and options. For programmatic access, use `get_thumbnailer`.

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

view raw JSON →