Skip to main content

A Django middleware to optimize image uploads by converting them to WebP.

Project description

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

Project details


Download files

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

Source Distribution

django_optimize_image-0.3.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

django_optimize_image-0.3.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file django_optimize_image-0.3.1.tar.gz.

File metadata

  • Download URL: django_optimize_image-0.3.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_optimize_image-0.3.1.tar.gz
Algorithm Hash digest
SHA256 23fd55adb6d030f9eda6546e24b08fb382b0312ffd74cd3a731e59bf4cb3e478
MD5 2487521cd3aa7e993fb26ec552405a11
BLAKE2b-256 d6f6cf4befb2fa9bb8a4122f5f3ef54826e40c476d5fa7254fddfa8aab854b8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_optimize_image-0.3.1.tar.gz:

Publisher: publish_manual.yml on nahom-d54/django-optimize-image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_optimize_image-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_optimize_image-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fae71d23185017376b204223cce77d734b88c89637b0ceff6096a1b67b659bcb
MD5 0432cf2a7541b6282f0ee9fe5c4a9bda
BLAKE2b-256 39c1c95ca80e58c881ae2aeb76b121fbc64d2916356ab4c5d978d8f8d91dca7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_optimize_image-0.3.1-py3-none-any.whl:

Publisher: publish_manual.yml on nahom-d54/django-optimize-image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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