Skip to main content

django-ok-images PyPI version

Build status Code health Python versions PyPI downloads Software license Project Status

WebP sizers and filters for django-versatileimagefield. Custom image field with direct access to created images through readable names and model mixin to inherit. Helper utils to delete created images, clear cache and warm images.

Installation

Install with pip:

$ pip install django-ok-images django-versatileimagefield

Update INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'versatileimagefield',
    ...
]

Available settings

IMAGE_ALLOWED_EXTENSIONS - Extensions for OptimizedImageField’s FileExtensionValidator.

IMAGE_MAX_FILE_SIZE - Max file size of uploaded images in megabytes. Default to 10.

IMAGE_OPTIMIZE_QUALITY - Quality to optimize an uploaded image.

IMAGE_CREATE_ON_DEMAND - Custom value for django-versatileimagefield create_images_on_demand setting.

IMAGE_PLACEHOLDER_PATH - Default placeholder path for django-versatileimagefield.

IMAGE_RGBA_CHANGE_BACKGROUND - Changes background of RGBA images to white color.

IMAGE_LOSSLESS - Image lossless configuration. Default to False.

How to enable image optimization through TinyPNG:

TINYPNG_API_KEY_FUNCTION - Path to function, which returns TinyPNG api key.

TINYPNG_API_KEY - TinyPNG api key.

How to use

WebP sizers and filter:

Add next file in any app to register sizers and filters (more about sizers and filters):

# versatileimagefield.py

from ok_images.contrib.versatileimagefield.versatileimagefield import *

Fields:

There is an OptimizedImageField, inherited from VersatileImageField.

Example of usage:

Add next settings (more about rendition key sets):

# settings.py

VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {
    'product': [
        ('full_size', 'url'),
        ('desktop', 'crop__460x430'),
        ('catalog_preview', 'crop__180x180'),

        # webp
        ('desktop_webp', 'crop_webp__460x430'),
        ('catalog_preview_webp', 'crop_webp__180x180'),
    ],
}

Define a model like this:

# models.py
from ok_images.fields import OptimizedImageField


class Product(models.Model):
    image_sizes = 'product'  # could be set as a global rendition key set for an each image field

    image = OptimizedImageField(
        _('Image'),
        ppoi_field='ppoi',
        blank=True,
        null=True,
        # Optional keyword arguments with default values
        image_sizes_serializer=VersatileImageFieldSerializer,  # from versatileimagefield.serializers import VersatileImageFieldSerializer
        image_sizes='product',  # some of keys, defined in VERSATILEIMAGEFIELD_RENDITION_KEY_SETS setting
        create_on_demand=True,  # enables or disables on-demand image creation
    )
    ppoi = PPOIField(
        verbose_name=_('PPOI')
    )

If image_sizes is not defined, uses next default rendition key set:

IMAGE_DEFAULT_RENDITION_KEY_SET = [
    ('full_size', 'url'),
]

How to access generated previews:

product.image.full_size
product.image.catalog_preview
product.image.desktop_webp

Utils:

delete_all_created_images - delete all created images (can be skipped with delete_images argument) and clear cache for passed models.

warm_images - creates all sized images for a given instance or queryset with passed rendition key set.

# anywhere.py
from ok_images.utils import delete_all_created_images, warm_images


delete_all_created_images(Product, delete_images = False)
warm_images(product, 'product')

# `rendition_key_set` could be taken from field's or model's attrbiute `image_sizes`, otherwise uses default key set
warm_images(Product.objects.all())

Async image warming:

tasks.py:

from ok_images.utils import warm_images

@app.task
def images_warmer_task(product_pk: int):
    from store.models import Product
    product = Product.objects.get(pk=product_pk)
    warm_images(
        instance_or_queryset=product,
        image_attr='image'
    )

models.py:

from .tasks import images_warmer_task


def images_warmer(product):
    images_warmer_task.delay(product.pk)


class Product(models.Model):
    image_sizes = 'product'

    image = OptimizedImageField(
        _('Image'),
        ppoi_field='ppoi',
        blank=True,
        null=True,
        images_warmer=images_warmer
    )

Metadata

Release files for django-ok-images 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-ok-images 0.1.8
File Size Uploaded Provenance Actions
django-ok-images-0.1.8.tar.gz 14.3 kB Without provenance Details for django-ok-images-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-ok-images 0.1.8
File Interpreter ABI Platform Actions
django_ok_images-0.1.8-py3-none-any.whl Python 3 none any Details for django_ok_images-0.1.8-py3-none-any.whl

Total release size: 32.0 kB

Release files / django-ok-images-0.1.8.tar.gz

Download URL django-ok-images-0.1.8.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5b7ada1167b4d2347e37b81037ef25f47d7318d5688f15301a430219ca6104b6
BLAKE2b-256 checksum
How to use checksums
66c41f1f03e43c4f8cb79bf707d4bc63ddbe93cfde0a320da88d8126841cc36d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / django_ok_images-0.1.8-py3-none-any.whl

Download URL django_ok_images-0.1.8-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7fb2bbf959a2f8eb0f5d910521b30ce87af7d1f3a950b08919bca006a8fb7b45
BLAKE2b-256 checksum
How to use checksums
c4f29f8fe6cb346b329db661abf5868657392e11a053456618e8933a24ebe9e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Security for django-ok-images 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page