Skip to main content

Image optimizer for Django.

Project description

Django Image Optimizer

pytest   pypi   mit-license

Django Image Optimizer converts images to WebP format while allowing you to specify the quality of the image. Optimized images that are larger than their originals are automatically deleted.

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 containing the images that you would like to optimize. django_img_optimizer will search through all subfolders for the specified image types.

Example:

If you have a folder inside static called images:

OPTIMIZE_IMAGE_ROOT = PROJECT_DIR / "static" / "images"

Note: The path above uses a Path object (from pathlib import Path).


Optional Settings

Image Quality

OPTIMIZE_IMAGE_QUALITY specifies the optimization image quality on a scale from 0 (lowest) 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>

You can use auto to generated the value of the alt and title attribute automatically using its filename.

Example:

{% optimized_image loading="lazy" decoding="async" src="images/logo-auto-attributes.jpg" alt="auto" title="auto" %}

This will render alt="Logo Auto Attributes" and title="Logo Auto Attributes".

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:

index.html

{% include 'partials/include.html' with image_name='logo' %}

include.html

{% with src="images/"|add:image_name|add:".jpg" %}
    {% optimized_image src=src %}
{% endwith %}

This will render:

<picture>
    <source srcset="/static/images/logo.webp" type="image/webp">
    <img src="/static/images/logo.jpg">
</picture>

Code Breakdown:

src is set to: src="images/

|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"

Data attributes and hyphens

Since Django template tags cannot accept attributes with hyphens (-) directly, you can use underscores (_) instead. The optimized_image tag will automatically convert underscores in attribute names to hyphens in the rendered HTML.

Example:

{% optimized_image src="images/logo.jpg" class="img-fluid" data_bs_toggle="modal" data_bs_target="#exampleModal" %}

This will render:

<picture>
    <source srcset="/static/images/logo.webp" type="image/webp">
    <img src="/static/images/logo.jpg" class="img-fluid" data-bs-toggle="modal" data-bs-target="#exampleModal">
</picture>

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_img_optimizer-1.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_img_optimizer-1.5-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django_img_optimizer-1.5.tar.gz.

File metadata

  • Download URL: django_img_optimizer-1.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_img_optimizer-1.5.tar.gz
Algorithm Hash digest
SHA256 dd3b327f2a79216a369f754e24d5cf687588fc5aeb76f9cb81eb775aabcc5e6a
MD5 7dd7a3ad774b64592089739838d8566d
BLAKE2b-256 08b39c283a64aa23c24ad3ba585f376af9264f827ef1eb5bab3a613ef939ae5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_img_optimizer-1.5.tar.gz:

Publisher: build.yml on peterstavrou/django-img-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_img_optimizer-1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_img_optimizer-1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 41bfb62f8099ed5aacd944ecb2c483bfe032ff250841392b3ccc33920cc4e898
MD5 78910dcbc2ac0f1874e109146809beb5
BLAKE2b-256 14c7f4f54bb3e02bf3845f50fe82bbdfe74bdba9353d16fcf9c0fd7810957f73

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_img_optimizer-1.5-py3-none-any.whl:

Publisher: build.yml on peterstavrou/django-img-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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