django-ok-images allows to generate WebP images using django-versatileimagefield.
Project description
WebP extensions for django-versatileimagefield.
Installation
Install with pip:
$ pip install django-ok-images
Available settings
IMAGE_ALLOWED_EXTENSIONS - Extensions for OptimizedImageField’s FileExtensionValidator.
IMAGE_OPTIMIZE_QUALITY - Quality to optimize an uploaded image.
IMAGE_CREATE_ON_DEMAND - Custom value for django-versatileimagefield create_images_on_demand setting.
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 details):
# versatileimagefield.py
from shared.ok_images.contrib.versatileimagefield.versatileimagefield import *
Fields:
There is an OptimizedImageField, inherited from VersatileImageField.
Example of usage:
# 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'),
],
}
# models.py
class Product(models.Model):
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.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.
delete_all_created_images(Product, delete_images = True)
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())
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.