Django Image Optimizer
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.11+
- Django 4.2+
- Pillow 13.0+ 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.
Author
Created by Peter Stavrou.
Metadata
Release files for django-img-optimizer 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_img_optimizer-2.0.1.tar.gz | 10.8 kB | With provenance | Details for django_img_optimizer-2.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_img_optimizer-2.0.1-py3-none-any.whl | Python 3 | none | any | Details for django_img_optimizer-2.0.1-py3-none-any.whl |
Total release size: 20.1 kB
Release files / django_img_optimizer-2.0.1.tar.gz
| Download URL | django_img_optimizer-2.0.1.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3c171d8c8f5c53f77ad93b4d4ceb3c704aa89a8f0db2224249103f35bd6e22a
|
|
BLAKE2b-256 checksum How to use checksums |
7b6c4876d6bccd6512507d3aac9f356b121b9449f862dfa441d2fb1262bb9702
|
| 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 logRelease files / django_img_optimizer-2.0.1-py3-none-any.whl
| Download URL | django_img_optimizer-2.0.1-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db98c75af08261dfce36b2b1f09958f1b872ff785268c0afab2755fcbac78503
|
|
BLAKE2b-256 checksum How to use checksums |
d978bc6b9468f1b0cf787ef19c5f7fa7975b8a2da8311b1cc17258048bfaec47
|
| 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 logSecurity for django-img-optimizer 2.0.1
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.
Attestation for django_img_optimizer-2.0.1.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 logA 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.