Image optimizer for Django.
Project description
Django Image Optimizer
Django Image Optimizer converts images to WebP format with a specified quality and automatically deletes the optimized image if it's larger than the original. This ensures efficient image optimization.
Installation
pip install django-img-optimizer
Configuration
Add django_img_optimizer to INSTALLED_APPS in your projects settings.py file:
INSTALLED_APPS = [
...
'django_img_optimizer',
...
]
Set OPTIMIZE_IMAGE_ROOT to the top-level folder which contains the images that you would like to optimize. django_img_optimizer will traverse through all of the subfolders looking for the specified image types.
For example, if you have a folder inside static called images:
OPTIMIZE_IMAGE_ROOT = os.path.join(PROJECT_DIR, 'static', 'images')
Optional Settings
Image Quality
OPTIMIZE_IMAGE_QUALITY specifies the optimization image quality on a scale from 0 (worst) to 100 (best) The lower the quality the smaller the size of the file.
Default is 100.
Example:
OPTIMIZE_IMAGE_QUALITY = 100
Image Types
OPTIMIZE_IMAGE_TYPES specifies a list of image types that you want to optimize.
Default is ['jpg', 'jpeg', 'png'].
Example:
OPTIMIZE_IMAGE_TYPES = ['jpg', 'jpeg', 'png']
Exclude Folders
OPTIMIZE_IMAGE_EXCLUDED_FOLDERS is used to exclude specific folders from the optimization process.
Default is None.
Example:
OPTIMIZE_IMAGE_EXCLUDED_FOLDERS = ['backup-larger-versions']
Usage
Command line
Optimize all images:
python manage.py optimize_images
Delete all optimized images:
python manage.py optimize_images --delete
Django templates
Load the optimized images in Django templates:
{% load image_optimizer %}
{% optimized_image 'loading="lazy"' 'decoding="async"' 'src="images/logo.jpg"' 'alt="Logo"' 'class="img-fluid"' %}
This will render:
<picture>
<source srcset="/static/images/logo.webp" type="image/webp">
<img loading="lazy" decoding="async" src="/static/images/logo.jpg" alt="Logo" class="img-fluid">
</picture>
Pass in any <img> attribute by wrapping it between single quotes '.
You can use variables with the optimize_image template tag that have been passed through include by using the built-in Django filter add.
Example:
In index.html
{% include 'partial/example.html' with image_name='logo' %}
In example.html
{% with src='"images/'|add:image_name|add:'.jpg"' %}
{% optimized_image 'src='|add:src %}
{% endwith %}
This will render:
<picture>
<source srcset="/static/images/logo.webp" type="image/webp">
<img src="/static/images/logo.jpg">
</picture>
Further explanation of example using variables with include:
src is set to: src='"images/'
The single quotes ' around "images/ indicates that it is a string.
|add:image_name adds the image_name variable to the string, resulting in: src="images/logo
|add:'.jpg"' adds .jpg" to the string, resulting in: src="images/logo.jpg"
AVIF Image Support
django_img_optimizer utilizes python-pillow, which currently lacks official support for AVIF files.
However, there is a pull request in progress to enable this support.
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_img_optimizer-1.0.tar.gz.
File metadata
- Download URL: django_img_optimizer-1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dfb17d834120d6fc5f1e8c6930f748af5e203d17261069b72a73345c3453879
|
|
| MD5 |
327b5628e2b436e6b728827d8d491e64
|
|
| BLAKE2b-256 |
3e904f48aafbefe451d6558b11729dfae1ccc2e27a57bfaf314db77d7646a4de
|
File details
Details for the file django_img_optimizer-1.0-py3-none-any.whl.
File metadata
- Download URL: django_img_optimizer-1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
218f57403e4561c7d7a3d4bf087e907eb2edc18bcaeb8dd43bcf0f8d5f53308a
|
|
| MD5 |
e8d022740e7497b53daf61d22fe000d8
|
|
| BLAKE2b-256 |
39a065b3e36d87a6af15ff6e8fc1c2b9e7bcc270a9cc0a5cac739b3fd3b77371
|