Open In App

Generate square or circular thumbnail image with Python – Pillow

Prerequisites: Pillow, Numpy

In the word “THUMBNAIL” the word THUMB means short. A thumbnail is the compressed preview image of the original image or a Thumbnail is the smaller version of an image. In simple words, the thumbnail is the smaller image that represents the larger/original image. 



Usually, the shape of the thumbnail is dependent on the original image, but in this article, we are going to generate the circular and square thumbnail image using Pillow Library in Python.

Installation:



For installing pillow and NumPy libraries, write the following command on your command prompt.

pip install pillow
pip install numpy

Example 1: Opening image using Pillow Library.




# importing necessary libraries
from PIL import Image
  
# opening the image from the storage using 
# Image.open() function
orig_img=Image.open('Geekwork/sebastian-molina.jpg')
  
# showing the image using show() function
orig_img.show()

Output:

Example 2: Generating circular thumbnail image using Pillow Library.

Approach:

Below is the full implementation:




# importing necessary libraries
from PIL import Image, ImageDraw
import numpy as np
  
# opening the image from
# the storage using Image.open() function
orig_img=Image.open('sebastian-molina.jpg')
  
# getting height and width of 
# an image using size() function
height,width=orig_img.size
  
# converting image to numpy array
npImage=np.array(orig_img)
  
# Creating mask image in L mode with same image size
new_img = Image.new('L', orig_img.size,0)
  
# drawing on mask created image using Draw() function
draw = ImageDraw.Draw(new_img)
  
# making circle on mask image using pieslice() function
draw.pieslice([0,0,height,width],0,360,fill=255)
  
# Converting the mask Image to numpy array
np_new=np.array(new_img)
  
# stack the array sequence
# (original image array with mask image) depth wise
npImage=np.dstack((npImage,np_new))
  
# converting array to an image using fromarray() function
final_img = Image.fromarray(npImage)
  
# making thumbnail using thumbnail() 
# function by passing the size in it
final_img.thumbnail((500,500))
  
# saving the circular thumbnail Image
final_img.save('Circular_thumbnail.png')

Output:

Example 3: Generating square thumbnail image using Pillow Library.

Approach:

Below is the full implementation:




# importing necessary libraries
from PIL import Image, ImageDraw
import numpy as np
  
# function to generate squared image
def square_thumb(thum_img,width,height):
    
    # checking if height and width are
    # are equal then return image as it is
    if height == width:
        return thum_img
  
    # checking if height is greater than width
    elif height > width:
        
        # creating the new mask image of size i,e height of Image
        square_img = Image.new(thum_img.mode, (height, height))
          
        # pasting the original image on mask image
        # using paste() function to make it square
        square_img.paste(thum_img, ((height - width) // 2,0))
          
        # returning the generated square image
        return square_img
  
    # if width is greater than height
    else:
        
        # creating the new mask image of size i,e width of Image
        square_img = Image.new(thum_img.mode, (width, width))
          
        # pasting the original image on mask image using paste()
        # function to make it square
        square_img.paste(thum_img, (0, (width - height) // 2))
          
        # returning the generated square image
        return square_img 
  
# main function  
if __name__ == "__main__":
    
    # opening the image from the storage
    # using Image.open() function
    orig_img=Image.open('sebastian-molina.jpg')
  
    # extracting width and height of an
    # image from the image size 
    w,h = orig_img.size
  
    # calling the function by passing
    # image width and height as a parameter
    sq_img = square_thumb(orig_img,w,h)
  
    # generating square thumbnail of
    # required size using thumbnail() function
    sq_img.thumbnail((400,400))
  
    # saving the thumbnail using save function
    sq_img.save('Square_thumbnail.jpg')

Output:

Explanation: We had created the function to generate a square image by passing the image, it’s width and height as a parameter. In the function we had checked three conditions on the basis of that we had generated the square image.


Article Tags :