Resizing images and converting formats in Django
Resizing and converting images in django doesn't have to be a chore with complex functions. This can be done quite simply in the model field definition.
Say hello to django_reized
First install the package:
pip install django-resized
Define your image field in the model using the ResizedImageField:
class Profile(models.Model): image = ResizedImageField(size=[300, 300], quality=75, upload_to="profiles/", force_format='WEBP', blank=True)
Now lets take a look at what this is doing.
Setting max width and max height of the file. If None is specified, it will keep the original size.
Specifying the quality of the image desired, scale 0-100.
The folder name for the file to go into on which ever static hosting site you are using.
Desired format we want to convert the images to, WebP being next gen format.
And there you have it, files will now be resized and converted to a specific format when uploading. I do not own this package but came across it a few months ago and have found it an absolute gem.