65

I want to resize an image in pillow-python, however I have 2 functions of choice to use:

Image.resize http://pillow.readthedocs.org/en/latest/reference/Image.html#PIL.Image.Image.resize

and

Image.thumbnail http://pillow.readthedocs.org/en/latest/reference/Image.html#PIL.Image.Image.thumbnail

Both definitions point out to resizing the image, Which one should I be using?

kguest
  • 3,804
  • 3
  • 29
  • 31
wolfgang
  • 7,281
  • 12
  • 44
  • 72

2 Answers2

88

Image.resize resizes to the dimensions you specify:

Image.resize([256,512],PIL.Image.ANTIALIAS) # resizes to 256x512 exactly

Image.thumbnail resizes to the largest size that (a) preserves the aspect ratio, (b) does not exceed the original image, and (c) does not exceed the size specified in the arguments of thumbnail.

Image.thumbnail([256, 512],PIL.Image.ANTIALIAS) # resizes 512x512 to 256x256

Furthermore, calling thumbnail resizes it in place, whereas resize returns the resized image.

max
  • 49,282
  • 56
  • 208
  • 355
wolfgang
  • 7,281
  • 12
  • 44
  • 72
  • 28
    `thumbnail` also does not enlarge the image. So e.g. an image of the size 150x150 will keep this dimension if `Image.thumbnail([512,512],PIL.Image.ANTIALIAS)` is called. – Hein van Dyke Aug 08 '17 at 14:12
  • 6
    Thanks. "Resizes in place" was the key to my issue. I was converting code to use thumbnail instead of resize and I was getting a null return. – Vaughn Apr 08 '18 at 02:27
  • Also, `thumbnail` preserves the aspect ratio. So applying `thumbnail(100, 200)` to a 512x512 image would change the image size to 100x100. – max Jan 12 '20 at 12:10
  • @HeinvanDyke thanks for the answer. is there a difference in quality when resizing, i.e., does one introduce artifacts while another doesn't? – Crashalot Aug 11 '22 at 07:20
4

Two examples for thumbnailing, one taken from geeksforgeeks:

# importing Image class from PIL package  
from PIL import Image 
   
# creating a object  
image = Image.open(r"C:\Users\System-Pc\Desktop\python.png") 
MAX_SIZE = (100, 100) 
  
image.thumbnail(MAX_SIZE) 
  
# creating thumbnail 
image.save('pythonthumb.png') 
image.show()

The second example relates to Python/Django. If you do that on a django model.py, you modify the def save(self,*args, **kwargs) method - like this:

class Profile(models.Model):                                                                                                                                                                                                                               
    user=models.OneToOneField(User, on_delete=models.CASCADE)                                                                                                                                                                                              
    image=models.ImageField(default='default.jpg', upload_to='img_profile')                                                                                                                                                                                                                                                                                                                                                                                   
    def __str__(self):                                                                                                                                                                                                                                     
        return '{} Profile'.format(self.user.email)                                                                                                                                                                                                    
    # Resize the uploaded image                                                                                                                                                                                                                            
    def save(self, *args, **kwargs):                                                                                                                                                                                                                       
        super().save(*args, **kwargs)                                                                                                                                                                                                                      
        img=Image.open(self.image.path)                                                                                                                                                                                                                    
        if img.height > 100 or img.width >100:                                                                                                                                                                                                             
            Max_size=(100,100)                                                                                                                                                                                                                             
            img.thumbnail(Max_size)                                                                                                                                                                                                                        
            img.save(self.image.path)                                                                                                                                                                                                                      
        else:                                                                                                                                                                                                                                              
            del img

In the last example both images remain stored in the file system on your server.