django-ok-images allows to generate WebP images using django-versatileimagefield.
Project description
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
)
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file django-ok-images-0.1.8.tar.gz
.
File metadata
- Download URL: django-ok-images-0.1.8.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b7ada1167b4d2347e37b81037ef25f47d7318d5688f15301a430219ca6104b6 |
|
MD5 | de30f3bc9786a0d5eee8b96c4046eb84 |
|
BLAKE2b-256 | 66c41f1f03e43c4f8cb79bf707d4bc63ddbe93cfde0a320da88d8126841cc36d |
File details
Details for the file django_ok_images-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: django_ok_images-0.1.8-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fb2bbf959a2f8eb0f5d910521b30ce87af7d1f3a950b08919bca006a8fb7b45 |
|
MD5 | b1cbd2265f2c4cf6561337693e5bd3d6 |
|
BLAKE2b-256 | c4f29f8fe6cb346b329db661abf5868657392e11a053456618e8933a24ebe9e3 |