Skip to main content

Django Image Optimizer

A Django middleware and utility for optimizing uploaded images by converting them to WebP with configurable width, height, and quality settings.

Features

✔ Automatic optimization via middleware
✔ Manual optimization via utility function
✔ Supports per-request overrides
✔ Configurable defaults via settings.py


Installation

pip install django-optimize-image

Add the middleware:

MIDDLEWARE = [
    # other middleware...
    "optimizer_middleware.middleware.ImageOptimizationMiddleware",
]

Configuration

Set global defaults in settings.py:

# Default image optimization settings
IMAGE_OPTIMIZER_WIDTH = 800   # Resize width
IMAGE_OPTIMIZER_HEIGHT = None  # Keep aspect ratio
IMAGE_OPTIMIZER_QUALITY = 80  # WebP quality

By default, images are resized to 800px width while maintaining aspect ratio and optimized to 80% quality.


Usage

1️⃣ Automatic Optimization (Middleware)

Once added to MIDDLEWARE, the optimizer automatically processes all uploaded images in POST, PUT, and PATCH requests.

✅ Converts images to WebP
✅ Resizes based on settings.py


2️⃣ Dynamic Overrides (Per Request)

Override optimization settings dynamically in a view using request.META:

def upload_view(request):
    request.META["IMAGE_OPTIMIZER_WIDTH"] = 600
    request.META["IMAGE_OPTIMIZER_QUALITY"] = 90
    return some_response

3️⃣ Manual Optimization (Standalone Function)

Use the function anywhere in your code (views, forms, serializers, etc.).

from optimizer_middleware.utils import optimize_image

optimized_image = optimize_image(image_file, width=500, quality=85)

License

MIT License. Free to use and modify. 🚀


Contribution

Use this guide Contribution

Metadata

Release files for django-optimize-image 0.3.1

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-optimize-image 0.3.1
File Size Uploaded Provenance Actions
django_optimize_image-0.3.1.tar.gz 5.5 kB With provenance Details for django_optimize_image-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-optimize-image 0.3.1
File Interpreter ABI Platform Actions
django_optimize_image-0.3.1-py3-none-any.whl Python 3 none any Details for django_optimize_image-0.3.1-py3-none-any.whl

Total release size: 12.2 kB

Release files / django_optimize_image-0.3.1.tar.gz

Download URL django_optimize_image-0.3.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
23fd55adb6d030f9eda6546e24b08fb382b0312ffd74cd3a731e59bf4cb3e478
BLAKE2b-256 checksum
How to use checksums
d6f6cf4befb2fa9bb8a4122f5f3ef54826e40c476d5fa7254fddfa8aab854b8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 May 26, 2026.

Transparency log

Release files / django_optimize_image-0.3.1-py3-none-any.whl

Download URL django_optimize_image-0.3.1-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fae71d23185017376b204223cce77d734b88c89637b0ceff6096a1b67b659bcb
BLAKE2b-256 checksum
How to use checksums
39c1c95ca80e58c881ae2aeb76b121fbc64d2916356ab4c5d978d8f8d91dca7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 May 26, 2026.

Transparency log

Security for django-optimize-image 0.3.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.

Select from 2 files

Attestation for django_optimize_image-0.3.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 May 26, 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