How To Use Python Pillow To Implement Image Zoom Operation, Batch Scale Image Size, And Create Image Thumbnail

This article will tell you how to use the Python pillow module Image class’s method resize() to implement a single image zoom operation and how to use it to batch modify multiple images size. It will also tell you how to create an image thumbnail using the pillow Image class’s method thumbnail().

1. Pillow Image Class’s resize() Function Introduction.

  1. To use the python pillow module Image class, you should first import it as below.
    from PIL import Image
  2. Below is the Image class’s resize() function syntax format, we will introduce each of the parameters one by one.
    resize(size, resample=image.BICUBIC, box=None, reducing_gap=None)
  3. size: tuple type parameter (width, height), the size of the image after scaling.
  4. resample: optional parameter, it refers to the image resampling filter, which is similar to the resample parameter of thumbnail(). The default value is image.BICUBIC.
  5. box: zoom the specified picture area. The parameter value of the box is the pixel coordinate tuple with a length of 4, that is (left, top, right, bottom). Note that the designated area must be within the scope of the original image. If it exceeds the scope, an error will be reported. When this parameter is not transmitted, the entire original image will be scaled by default.
  6. reducing_gap: optional parameter, floating-point type value, which is used to optimize the zoom effect of the picture. The common parameter values are 3.0 and 5.0.
  7. The method resize() returns a new Image object.

2. Scale Image Size Using Python Pillow Example.

  1. The below source code will scale the source image to the target image with the specified image width and height.
    from PIL import Image
    
    # this function will scale the source file to the target file with the specified width and height.
    # it will also scale part of the source image and show it to the user.
    def scale_image_size(source_image_file_path, new_img_width, new_img_height, target_image_file_path):
        
        # open the source image object.
        source_img_obj = Image.open(source_image_file_path)
        
        # scale the image to the new size, it will return a new Image object.
        scaled_img = source_img_obj.resize((new_img_width, new_img_height))
        
        # save the scaled image to a new file.
        scaled_img.save(target_image_file_path)
               
        # get the source image size.
        source_image_size = source_img_obj.size
            
        # get the scaled image size.
        scaled_img_size = scaled_img.size
    
        # print the source image size.
        print("The source image size is :", source_image_size)
        
        # print the scaled image size.
        print("The scaled image size is :", scaled_img_size)
    
        # scale part of the source image.
        scaled_special_area_img = source_img_obj.resize((new_img_width,new_img_height),resample=Image.LANCZOS,box=(10,10,200,300))
        
        # display the scaled part image.
        scaled_special_area_img.show()
    
    if __name__ == '__main__':
        
        source_image_file_path = '/Users/songzhao/Desktop/matplotlib-set-text.webp'
        
        target_image_file_path = '/Users/songzhao/Desktop/matplotlib-set-text-scaled.webp'
        
        new_img_width = 800
        
        new_img_height = 600
        
        scale_image_size(source_image_file_path = source_image_file_path, new_img_width = new_img_width, new_img_height = new_img_height, target_image_file_path = target_image_file_path)
    
    

3. Batch Scale Image Size.

  1. The below source code will batch scale all the images in the provided folder.
    from PIL import Image
    
    import os
        
    def batch_scale_image_size(image_folder_path, new_img_width, new_img_height):
        
       # get the image files name list in the source folder.  
       image_file_list = os.listdir(image_folder_path)
       
       # loop in the image file names list.
       for image_file in image_file_list:
           
           # open the source image.
           source_image = Image.open(image_folder_path + image_file)
        
           # resize source image and create a new image. 
           target_image = source_image.resize((new_img_width, new_img_height),Image.BILINEAR)
           
           # show the target image.
           target_image.show()
        
           # save the scaled target image to a new image file.
           target_image.save(image_folder_path + 'scaled_'+image_file)
       
       
    
    if __name__ == '__main__':
        
        img_folder_path = '/Users/songzhao/Desktop/test/'
        
        new_img_width = 800
        
        new_img_height = 600
        
        batch_scale_image_size(image_folder_path = img_folder_path, new_img_width = new_img_width, new_img_height = new_img_height)
    

4. Use Python Pillow Module To Create Image Thumbnails.

  1. The Image class’s thumbnail() method is used to create thumbnail images.
  2. Below is the syntax format of the thumbnail() method.
    thumbnail(size,resample)
  3. size: a tuple type parameter, refers to the thumbnail image size.
  4. resample: this is an optional parameter, it defines the image resampling filter. There are four filtering types ( PIL.Image.BICUBIC (the default value), PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.LANCZOS ).
  5. Below is the example source code to create an image’s thumbnail.
    from PIL import Image
       
    # create image thumbnail with the provided source image path, thumbnail image width & height.
    def create_thumbnail_image(source_image_file_path, thumbnail_width, thumbnail_height):
        
        # open the source image.
        img_obj = Image.open(source_image_file_path)
    
        # create the image thumbnail.
        img_obj.thumbnail((thumbnail_width, thumbnail_height))
        
        thumbnail_img_size = img_obj.size
    
        print("Thumbnail image size : ",thumbnail_img_size)
        
        # display the image thumbnail.
        img_obj.show()
    
        # create the thumbnail image file name based on the source image file name.
        file_name_index = source_image_file_path.rindex('/')
        # get the source file path.
        file_path = source_image_file_path[:file_name_index+1]
        # get the source fila name.
        file_name = source_image_file_path[file_name_index+1:]
        # create the thumbnail file path.
        thumbnail_file_path = file_path + 'thumbnail_' + file_name
        
        # save the thumbnail image to a file.
        img_obj.save(thumbnail_file_path)   
    
    if __name__ == '__main__':
     
        source_image_file_path = '/Users/songzhao/Desktop/test/set-matplotlib-plot-axis-range.webp'
        
        new_img_width = 30
        
        new_img_height = 30
        
        create_thumbnail_image(source_image_file_path = source_image_file_path, thumbnail_width = new_img_width, thumbnail_height = new_img_height)
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.