Python Pillow – Creating Thumbnails


Python Pillow – Creating Thumbnails



”;


Thumbnails are typically used for displaying image previews or smaller representations of the original image. They are useful for optimizing web pages and improving the loading speed of image-heavy applications. Pillow provides a convenient way to generate thumbnails from images. Here are some key points about thumbnails in Pillow.

  • Preservation of Aspect Ratio − When creating a thumbnail Pillow maintains the aspect ratio of the original image. This means that the width and height of the thumbnail are adjusted in proportion to the original image so the image does not appear distorted.

  • Reduced File Size − Thumbnails are smaller in size compared to the original image. This reduction in size is useful for optimizing web pages or displaying images in constrained spaces such as in galleries or lists.

  • Convenience − It simplifies the process of creating thumbnails. It resizes the image while preserving the aspect ratio and it provides an easy way to save the resized image to a file.

  • Quality Control − We can control the quality of the thumbnail using various parameters such as the size, filter type for resizing and compression settings if we are saving the thumbnail in a compressed format like JPEG.

In pillow we have the method namely thumbnail() which allows us to specify the dimensions and shape for the thumbnail image. We can create the thumbnails in two different shapes one is square and the other is circle.

Creating square Thumbnails

In this chapter we are going to see how to create a square thumbnail by using the thumbnail() method of the pillow library.

The syntax and parameters for thumbnail() method is as follows.


image.thumbnail(size, resample=Image.BOX)

Where,

  • size (required) − This parameter specifies the dimensions i.e. width and height for the thumbnail as a tuple (width, height). We can also specify just one dimension and the other dimension will be automatically calculated to maintain the aspect ratio.

  • resample (optional) − This parameter defines the resampling filter to use when resizing the image. It can be one of the following constants −

    • Image.NEAREST (default) − Nearest-neighbor sampling.

    • Image.BOX − Box sampling, which is similar to the nearest-neighbor but generally gives slightly smoother results.

    • Image.BILINEAR − Bilinear interpolation.

    • Image.HAMMING − Hamming-windowed sinc interpolation.

    • Image.BICUBIC − Bicubic interpolation.

    • Image.LANCZOS − Lanczos-windowed sinc interpolation.

Example

In this example we are creating the square thumbnail by using the thumbnail() method by specifying the width and height parameters of the thumbnail to the resize parameter.


from PIL import Image

#Open the image
image = Image.open("Images/tutorialspoint.png")

#Define the thumbnail size as a tuple (width, height)
thumbnail_size = (200, 200)

#Create a thumbnail
image.thumbnail(thumbnail_size, resample = Image.BOX )
image.save("output Image/square_thumbnail_image.png")
image.show() 

Output


tutorialspoint

Example

Here is another example of creating a square thumbnail with width 100 and height as 100 by using the thumbnail() module.


from PIL import Image

#Open the image
image = Image.open("Images/butterfly.jpg")

#Define the thumbnail size as a tuple (width, height)
thumbnail_size = (100, 100)

#Create a thumbnail
image.thumbnail(thumbnail_size, resample = Image.Resampling.BILINEAR)
image.save("output Image/square_thumbnail_image.png")
image.show() 

Output


square thumbnail image

Creating circle Thumbnails

In the above section, we have gone through what is thumbnail and how to create the square thumbnail using the pillow thumbnail() method. Now, we are going to see the circle thumbnail creation. Circle thumbnails means the thumbnail will be in the circle shape.

The syntax and parameters of the thumbnail() method for creating the circle thumbnail are same as the square thumbnail.

Here are the steps to be followed to create the circular thumbnail.

  • Import the necessary modules Image and ImageDraw from the Pillow library.

  • Use the Image.open() method to load the original image.

  • Determine the dimensions of the original image using the size attribute.

  • Create a new object from the mask image using the ImageDraw.Draw() method.

  • Draw an ellipse on the mask image using the draw.ellipse() method. Centering the image to the center of the ellipse.

  • Create a new image with the same dimensions as the original image with a transparent background using the Image.new() method.

  • Use the save() method to save the circle thumbnail image.

  • Use the show() method to display the created circle thumbnail image.

Example

In this example we are creating the circular thumbnail by using the thumbnail() method of the pillow library.


#importing the required libraries
from PIL import Image, ImageDraw 

#open image file
img = Image.open(''Images/butterfly.jpg'')

#resize image
img.thumbnail((2000, 2000))

#create circular mask
mask = Image.new(''L'', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill = 255)

#apply mask to image
result = Image.new(''RGBA'', img.size, (255, 255, 255, 0))
result.paste(img, (0, 0), mask)

#save circular thumbnail image
result.save(''OutputImages/circular_thumbnail1.png'')

#showing the image using show() function
result.show()

Image to be used


butterfly original image

Output


circular thumbnail


















Advertisements

”;

Leave a Reply

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