Skip to main content

Django Image Optimizer

Tests PyPI MIT License

Django Image Optimizer generates AVIF and WebP alternatives for static JPEG and PNG images. It keeps an optimized file only when it is smaller than its source, then the template tag serves AVIF first, WebP second, and the original image as the fallback.

Requirements

  • Python 3.9+
  • Django 4.2+
  • Pillow 11.3+ with AVIF and WebP support

Installation

pip install django-img-optimizer

Add the app to INSTALLED_APPS:

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

Configuration

Set OPTIMIZE_IMAGE_ROOT to the top-level static-image directory. The command searches its subdirectories.

OPTIMIZE_IMAGE_ROOT = PROJECT_DIR / 'static' / 'images'

Optional settings:

# Integer from 0 to 100. These defaults provide comparable perceptual quality.
OPTIMIZE_IMAGE_AVIF_QUALITY = 61
OPTIMIZE_IMAGE_WEBP_QUALITY = 80

# Source extensions to convert. Default: ['jpg', 'jpeg', 'png'].
OPTIMIZE_IMAGE_TYPES = ['jpg', 'jpeg', 'png']

# Folder names to exclude, including every descendant folder. Default: [].
OPTIMIZE_IMAGE_EXCLUDED_FOLDERS = ['backup-larger-versions']

The command records generated files in .django-img-optimizer.json inside OPTIMIZE_IMAGE_ROOT. Do not edit or commit this generated file. Existing AVIF and WebP files not recorded in that manifest are never overwritten or deleted.

Source image stems must be unique in the same directory. For example, logo.jpg and logo.png cannot both be optimized because each would produce logo.avif and logo.webp.

Usage

Generate optimized images:

python manage.py optimize_images

Delete only files previously generated by this package:

python manage.py optimize_images --delete

Django templates

Load the tag and use a static path relative to your static directory:

{% load image_optimizer %}
{% optimized_image loading="lazy" decoding="async" src="images/logo.jpg" alt="Logo" class="img-fluid" %}

When both optimized files were generated, it renders:

<picture>
<source srcset="/static/images/logo.avif" type="image/avif">
<source srcset="/static/images/logo.webp" type="image/webp">
<img loading="lazy" decoding="async" src="/static/images/logo.jpg" alt="Logo" class="img-fluid">
</picture>

Use auto to derive alt and title values from the filename:

{% optimized_image src="images/logo-auto-attributes.jpg" alt="auto" title="auto" %}

This renders alt="Logo Auto Attributes" and title="Logo Auto Attributes".

Template variables work as normal:

{% with src="images/"|add:image_name|add:".jpg" %}
    {% optimized_image src=src %}
{% endwith %}

Use underscores for hyphenated HTML attributes. The tag converts them safely:

{% optimized_image src="images/logo.jpg" data_bs_toggle="modal" data_bs_target="#exampleModal" %}

This renders data-bs-toggle and data-bs-target on the <img> element.

Metadata

Release files for django-img-optimizer 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-img-optimizer 2.0.0
File Size Uploaded Provenance Actions
django_img_optimizer-2.0.0.tar.gz 10.8 kB With provenance Details for django_img_optimizer-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-img-optimizer 2.0.0
File Interpreter ABI Platform Actions
django_img_optimizer-2.0.0-py3-none-any.whl Python 3 none any Details for django_img_optimizer-2.0.0-py3-none-any.whl

Total release size: 20.1 kB

Release files / django_img_optimizer-2.0.0.tar.gz

Download URL django_img_optimizer-2.0.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f224b68ac7c362c3311fb1f216c892c532c93825612e91c9956b6f39a56721e4
BLAKE2b-256 checksum
How to use checksums
a9946929b1cc6ac5a9db076c13fb6a80b9d5058e1017b85a96af90fcf1c629c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

Release files / django_img_optimizer-2.0.0-py3-none-any.whl

Download URL django_img_optimizer-2.0.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a733b3e17e80b6b9a9d17094c7ab4d38d9b471f83f0c48038a3d25917a7e5859
BLAKE2b-256 checksum
How to use checksums
4428cf6b8808c75d732fabf1c8ddf2c3acf9ee6547b6f383ee626bc1b8e69676
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

Security for django-img-optimizer 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_img_optimizer-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page