Skip to main content

A Django app that automates thumbnail creation for image fields.

Project description

Django Advance Thumbnail

A Django app that automates thumbnail creation for image fields. It generates, updates, and deletes thumbnails based on the source image with support for custom sizes and resize methods.

Features

  • Automatic thumbnail generation on model save
  • Thread-safe for multi-threaded/parallel web servers (gunicorn, uwsgi, etc.)
  • Multiple resize methods: fit (maintain aspect ratio) or fill (exact dimensions)
  • Smart regeneration - only regenerates when source image or settings change
  • Management commands for bulk operations
  • Full Django migrations support

Installation

pip install django-advance-thumbnail

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'django_advance_thumbnail',
]

Quick Start

from django.db import models
from django_advance_thumbnail import AdvanceThumbnailField

class Product(models.Model):
    image = models.ImageField(upload_to='products/')
    thumbnail = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/',
        size=(300, 300),
        null=True,
        blank=True,
    )

Parameters

Parameter Type Default Description
source_field str required Name of the source ImageField
size tuple (300, 300) Thumbnail dimensions (width, height)
resize_method str 'fit' How to resize: 'fit', 'fill', or 'cover'
force_regenerate bool False Regenerate on every save

Resize Methods

fit (default)

Maintains aspect ratio. Thumbnail fits within the specified size but may be smaller in one dimension.

# A 400x100 image with size=(150, 150) becomes 150x37
thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(150, 150),
    resize_method='fit',  # default
)

fill / cover

Guarantees exact dimensions by cropping. Use this when you need thumbnails to be exactly the specified size.

# A 400x100 image with size=(150, 150) becomes exactly 150x150
thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(150, 150),
    resize_method='fill',  # guarantees 150x150
)

Usage Examples

Basic Usage

from django.db import models
from django_advance_thumbnail import AdvanceThumbnailField

class Article(models.Model):
    image = models.ImageField(upload_to='articles/')
    thumbnail = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/',
        size=(300, 300),
        null=True,
        blank=True,
    )

Multiple Thumbnails

class Product(models.Model):
    image = models.ImageField(upload_to='products/')

    # Small thumbnail for listings (fit mode)
    thumbnail_small = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/small/',
        size=(100, 100),
        resize_method='fit',
    )

    # Large thumbnail for detail page (exact dimensions)
    thumbnail_large = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/large/',
        size=(400, 400),
        resize_method='fill',
    )

Using Constants

from django_advance_thumbnail import AdvanceThumbnailField, RESIZE_FIT, RESIZE_FILL

thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(200, 200),
    resize_method=RESIZE_FILL,
)

Management Commands

Generate Thumbnails

Generate thumbnails for existing images (use when adding field to existing models):

# All models
python manage.py generate_thumbnails

# Specific model
python manage.py generate_thumbnails --model myapp.Product

# Force regenerate existing
python manage.py generate_thumbnails --force

# Preview without changes
python manage.py generate_thumbnails --dry-run

Regenerate Thumbnails

Regenerate thumbnails after changing size or resize_method:

# Detect and regenerate changed
python manage.py regenerate_thumbnails

# Force regenerate all
python manage.py regenerate_thumbnails --force

# Clear cache first
python manage.py regenerate_thumbnails --clear-cache

Smart Regeneration

Thumbnails are only regenerated when:

  • Source image changes
  • size parameter changes
  • resize_method parameter changes
  • Thumbnail doesn't exist

This is efficient for production - no unnecessary processing.

Thread Safety

Version 2.0+ is fully thread-safe for multi-threaded web servers like gunicorn and uwsgi. The previous signal disconnect/reconnect pattern has been replaced with instance-level flags.

Requirements

  • Python >= 3.6
  • Django >= 3.0
  • Pillow >= 8.0.0

Upgrading to v2.0

See MIGRATION_GUIDE.md for breaking changes and upgrade instructions.

Contact

License

MIT License

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_advance_thumbnail-2.0.0.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_advance_thumbnail-2.0.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file django_advance_thumbnail-2.0.0.tar.gz.

File metadata

  • Download URL: django_advance_thumbnail-2.0.0.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for django_advance_thumbnail-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b458e15d5c3c39d69a16192daa796d228af2a33bd618341f1d7bb2f72c931f62
MD5 25f70ccc238a3a08244a6c7cf681f1bd
BLAKE2b-256 f75bf8de4870049d3e66e9dff55321173619e204cc87225b5e8361b9140aca0d

See more details on using hashes here.

File details

Details for the file django_advance_thumbnail-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_advance_thumbnail-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 678386d0b7d84cac74b169515b4971123a9672e419d96e03fa213e6e2b7ed3d8
MD5 6b54e1a5753edad681d143bb3b181e0d
BLAKE2b-256 e2a585679a5c78b4b6ef4b57061e4fd6197505b0b5954859f6f43f736ca86a40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page