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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django-optimize-image-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4ddfee6d2a0876dc1d5594f0eea00459d5248936c86aac65f58c4d852882008a
MD5 aea9b802bfa7621c04a3a44095a4bb9e
BLAKE2b-256 4d7542413513669cf2d02a0f716272b6cb0292148aa8ee2556d874e1ae498873

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_optimize_image-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 050414a7e5e5d70d8bab087f870a8c1e2f3a4565b2d759a63a565ecc5d193457
MD5 fdcc6012a515841f615d408c390cea56
BLAKE2b-256 3101612dec012bdc0ad71e6b6c3d24446bc4ca75c9c88e97cf85c4afd914d81b

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