[Django]-How to resize and crop an image into a square in Django?

3๐Ÿ‘

You repeated the same condition blocks 3 times which makes your code hard to read and maintain.

The code below is the exact same procedure you follow without that repeat I mentioned.

from PIL import Image

class CustomUser(AbstractBaseUser, PermissionsMixin):
    # User model fields, etc
    image = models.ImageField(default='default.jpg',upload_to='profile_pics')

    def save(self, *args, **kwargs):
        super().save()
        img = Image.open(self.image.path)
        width, height = img.size  # Get dimensions

        if width > 300 and height > 300:
            # keep ratio but shrink down
            img.thumbnail((width, height))

        # check which one is smaller
        if height < width:
            # make square by cutting off equal amounts left and right
            left = (width - height) / 2
            right = (width + height) / 2
            top = 0
            bottom = height
            img = img.crop((left, top, right, bottom))

        elif width < height:
            # make square by cutting off bottom
            left = 0
            right = width
            top = 0
            bottom = width
            img = img.crop((left, top, right, bottom))

        if width > 300 and height > 300:
            img.thumbnail((300, 300))

        img.save(self.image.path)

1๐Ÿ‘

Try this.

from PIL import Image

class CustomUser(AbstractBaseUser, PermissionsMixin):
    # User model fields, etc
    image = models.ImageField(default='default.jpg',upload_to='profile_pics')

    def save(self, *args, **kwargs):
        super().save()
        img = Image.open(self.user_profile_img.path)

        # When image height is greater than its width
        if img.height > img.width:
            # make square by cutting off equal amounts top and bottom
            left = 0
            right = img.width
            top = (img.height - img.width)/2
            bottom = (img.height + img.width)/2
            img = img.crop((left, top, right, bottom))
            # Resize the image to 300x300 resolution
            if img.height > 300 or img.width >300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.user_profile_img.path)

        # When image width is greater than its height
        elif img.width > img.height:
            # make square by cutting off equal amounts left and right
            left = (img.width - img.height)/2
            right = (img.width + img.height)/2
            top = 0
            bottom = img.height
            img = img.crop((left, top, right, bottom))
            # Resize the image to 300x300 resolution
            if img.height > 300 or img.width >300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.user_profile_img.path)
๐Ÿ‘คBK94

0๐Ÿ‘

django-cropper-image is an app for client side cropping and compressing uploaded images via Djangoโ€™s app using with help cropper.js. github link django-cropper-image.

from django.db import models
from django_cropper_image.fields import ImageCropperField
class Images(models.Model):
    image = ImageCropperField(upload_to='image',max_length=255)
๐Ÿ‘คAneesh Usman

-1๐Ÿ‘

You can use Pillow to manipulate images in python. You can install it using pip. It has a lot of features which can help you in manipulating images. You can read more about Pillow here:

https://pypi.org/project/Pillow/

https://pillow.readthedocs.io/en/stable/

๐Ÿ‘คharriebird

Leave a comment