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-image-optimizer

Add to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # other apps...
    "optimizer_middleware",
]

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.1.0.tar.gz (4.4 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.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file django-optimize-image-0.1.0.tar.gz.

File metadata

  • Download URL: django-optimize-image-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for django-optimize-image-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a7e3e122a56300679f2de875ec12b7408051136d063406a5a8ce3c8f52d306bf
MD5 0136d87747b76e88f726b71054b1be8e
BLAKE2b-256 1c3e40bc5b850ac98d9338cbba41c605383e5264c4ff617859b11a84443b9803

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_optimize_image-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78b6a670d68a82eeaff7f4a91730ea02d8e3bb6273e7cdfe06c15efc11dfa636
MD5 9c883c1396b0bcebf560987a1fff7b9a
BLAKE2b-256 03034b310d7f8f5684f3fb4eafb4d82f6fc5e92878e7677bad1696ae6a034c90

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