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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23fd55adb6d030f9eda6546e24b08fb382b0312ffd74cd3a731e59bf4cb3e478
|
|
| MD5 |
2487521cd3aa7e993fb26ec552405a11
|
|
| BLAKE2b-256 |
d6f6cf4befb2fa9bb8a4122f5f3ef54826e40c476d5fa7254fddfa8aab854b8a
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_optimize_image-0.3.1.tar.gz -
Subject digest:
23fd55adb6d030f9eda6546e24b08fb382b0312ffd74cd3a731e59bf4cb3e478 - Sigstore transparency entry: 1633431754
- Sigstore integration time:
-
Permalink:
nahom-d54/django-optimize-image@fd6498a8cd3b00da76439d330c7b31455b23d340 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nahom-d54
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_manual.yml@fd6498a8cd3b00da76439d330c7b31455b23d340 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file django_optimize_image-0.3.1-py3-none-any.whl.
File metadata
- Download URL: django_optimize_image-0.3.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae71d23185017376b204223cce77d734b88c89637b0ceff6096a1b67b659bcb
|
|
| MD5 |
0432cf2a7541b6282f0ee9fe5c4a9bda
|
|
| BLAKE2b-256 |
39c1c95ca80e58c881ae2aeb76b121fbc64d2916356ab4c5d978d8f8d91dca7a
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_optimize_image-0.3.1-py3-none-any.whl -
Subject digest:
fae71d23185017376b204223cce77d734b88c89637b0ceff6096a1b67b659bcb - Sigstore transparency entry: 1633431798
- Sigstore integration time:
-
Permalink:
nahom-d54/django-optimize-image@fd6498a8cd3b00da76439d330c7b31455b23d340 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nahom-d54
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_manual.yml@fd6498a8cd3b00da76439d330c7b31455b23d340 -
Trigger Event:
workflow_dispatch
-
Statement type: