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_resized

First install the package and dependency:

pip install django-resized
pip install pillow

Define your image field in the model using the ResizedImageField:

from django_resized import 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.

size=[300x300]

Setting max width and max height of the file. If None is specified, it will keep the original size.

quality=75

Specifying the quality of the image desired, scale 0-100.

upload_to="profiles/"

The folder name for the file to go into on which ever static hosting site you are using.

force_format="WEBP"

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.

Image ResizingDjangoImage Conversion
Avatar for Daisy McGirr

Written by Daisy McGirr

Software Engineer & Student Mentor

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.