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.1.tar.gz (4.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.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-optimize-image-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 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.1.tar.gz
Algorithm Hash digest
SHA256 ba1d589c0a58d4378340a86851f53064563c7d724e566af263d9cc90fa37b042
MD5 65d957aad29d74d2445c952f6d09e924
BLAKE2b-256 ac4af72467de592f1989db4e46a959011a3c6141374ed656c1bb1a5a4bcf9e97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_optimize_image-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee9820d9b6ed25ba9c79351d6b4d4caae01c2dbe80a5874995f94896d738c9c5
MD5 f7eb011cd4bf1b636cbc89c80dd9a47d
BLAKE2b-256 2e1849fbb619e7040b5e500b41110d6280464e3d9a4216f2bfbdb5bb4061aa6a

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