https://github.com/nahom-d54/django-optimize-image
A Django middleware to optimize image uploads by converting them to WebP.
https://github.com/nahom-d54/django-optimize-image
django django-framework django-rest-framework framework middleware python web
Last synced: 12 months ago
JSON representation
A Django middleware to optimize image uploads by converting them to WebP.
- Host: GitHub
- URL: https://github.com/nahom-d54/django-optimize-image
- Owner: nahom-d54
- License: mit
- Created: 2025-03-25T12:04:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-25T19:53:49.000Z (about 1 year ago)
- Last Synced: 2025-04-29T19:53:26.041Z (12 months ago)
- Topics: django, django-framework, django-rest-framework, framework, middleware, python, web
- Language: Python
- Homepage: https://pypi.org/project/django-optimize-image/
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# **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**
```sh
pip install django-image-optimizer
```
Add to `INSTALLED_APPS` in `settings.py`:
```python
INSTALLED_APPS = [
# other apps...
"optimizer_middleware",
]
```
Add the middleware:
```python
MIDDLEWARE = [
# other middleware...
"optimizer_middleware.middleware.ImageOptimizationMiddleware",
]
```
---
## **Configuration**
Set global defaults in **`settings.py`**:
```python
# 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`**:
```python
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.).
```python
from optimizer_middleware.utils import optimize_image
optimized_image = optimize_image(image_file, width=500, quality=85)
```
---
## **License**
[MIT License](LICENSE). Free to use and modify. 🚀
---
## **Contribution**
Use this guide [Contribution](https://opensource.guide/how-to-contribute/)