Pillow-Python中Image.resize和Image.thumbnail有什么区别

时间:2015-03-31 11:51:09

标签: python python-imaging-library pillow

我想在pillow-python中调整图像大小,但是我有两个可供选择的功能:

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

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

两个定义都指出要调整图像的大小,我应该使用哪一个?

2 个答案:

答案 0 :(得分:35)

Image.resize调整为您指定的尺寸

Image.resize([256,512],PIL.Image.ANTIALIAS)#精确调整为256x512

Image.thumbnail调整最大输入尺寸(宽度和高度)

Image.thumbnail([512,512],PIL.Image.ANTIALIAS)

[512,512]是为图片大小调整提供的最大尺寸

此外,调用thumbnail会将其调整到适当的位置,而resize会返回已调整大小的图像。

答案 1 :(得分:1)

两个缩略图示例,一个取自 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()

第二个例子与 Python/Django 相关。 如果您在 django model.py 上执行此操作,则修改 def save(self,*args, **kwargs) 方法 - 如下所示:

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

在最后一个示例中,两个图像都存储在您服务器上的文件系统中。