Django PIL image not resizing

I am trying to resize the profile picture for my profile model. And I am not seeing what I am doing wrong. Below is the code:

def save(self, *args, **kwargs):
    super().save()

    img = Image.open(self.profile_picture.path)
    img.show()
    img_resized = img.resize((100, 100), Image.Resampling.LANCZOS)
    img_resized.save()

I would like the code to be resized at the dimensions mentioned in the code. I am receiving no errors in the terminal during runserver.

make sure you specify the path where you want to save the image img_resized.save() is missing the file path argument.

ensure that when calling super().save(), you pass *args and **kwargs to it as well.

from PIL import Image

class YourProfileModel(models.Model):
    profile_picture = models.ImageField(upload_to='profile_pics')

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

        img_resized = img.resize((100, 100), Image.LANCZOS)

        img_resized.save(self.profile_picture.path)
Вернуться на верх