Wasek Samin
Wasek Samin

Reputation: 33

Is there any way to forcibly resize an image to a given dimension in python?

I am using the pillow library. I have an image whose dimension is 1280x1920. When I upload the image, I want to resize the image to 800x600. But after uploading, the image resized to 475x350. Is there any way to forcibly resize the image into the given dimension? This is my code to resize the image in Django:

img = Image.open(self.image.path)

if img.width > 800 or img.height > 600:
    img.resize((800, 600), Image.ANTIALIAS)
    img.save(self.image.path)

Upvotes: 3

Views: 53

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

The resize() returns a copy of the modified image - it doesn't modify the passed in image in-situ, so you need:

img = Image.open(self.image.path)

if img.width > 800 or img.height > 600:
    img = img.resize((800, 600), Image.ANTIALIAS)     # save result of resize()
    img.save(self.image.path)

Upvotes: 2

Related Questions