Generate square or circular thumbnail image with Python - Pillow


Thumbnail images are commonly used in a variety of applications to present photos in a compact and visually appealing manner. They enable users to navigate through a huge number of photographs fast and effortlessly since they are compact, compressed images that faithfully depict the original image.

Python is popular for manipulation and processing of images. A well-known Python library for working with images is the Python Imaging Library (PIL), now called Pillow that offers a large number of tools for carrying out various image-processing operations, such as cropping, resizing, and applying filters.

Approaches

  • Generating a Square Thumbnail

  • Generating a Circular Thumbnail

  • Generating a Circular Thumbnail using ImageOps.fit()

Prerequisite

Before moving forward we have to do installation of some python libraries, we will be installing the Pillow and numpy library for the same using the below command:

pip install pillow
pip install numpy

Before looking at the approaches for Generating square or circular thumbnail images, first we need to know how to open a thumbnail image using the pillow library in python. Let's take an example:

Example

# importing the required libraries
from PIL import Image

# opening the image
image=Image.open('spiderman.jpg')

# resize image
img.thumbnail((width, height))

# save thumbnail image
img.save('thumbnail_image.jpg')

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

Output

We have successfully opened the thumbnail image using the above code.

Method 1: Generating a Square Thumbnail

Algorithm

  • Import the necessary modules from the Pillow library: Image.

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

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

  • By dividing width by height, determine the original image's aspect ratio.

  • Set the desired width or height (whichever is lower) as the square's side lengths to determine the size of the thumbnail image.

  • Calculate the size of the region of the original image to crop. If the aspect ratio of the original image is greater than 1 (i.e., it is wider than it is tall), crop the top and bottom of the image so that it has the same height as the width of the thumbnail. If the aspect ratio is less than 1 (i.e., it is taller than it is wide), crop the sides of the image so that it has the same width as the height of the thumbnail.

  • To crop a portion of the original image, use the crop() method.

  • Use the thumbnail() method to resize the cropped image to the desired size.

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

Step 1:- Import the Image module from the PIL (Python Imaging Library) package.

from PIL import Image 

Step 2:- Open the original image file.

img = Image.open('spiderman.jpg')

Step 3:- Resize the image, while preserving the aspect ratio.

img.thumbnail((2000, 2000))

Step 4:- Determine the dimensions of the image after resizing.

width, height = img.size

Step 5:- Calculate the coordinates of the region to crop in order to achieve a square shape, while preserving the center of the original image. If the original image is wider than its height, crop the left and right sides of the image. Otherwise, crop the top and bottom of the image. The crop() method takes the left, top, right, and bottom coordinates of the region to crop, so we can calculate these values.

if width > height:
   left = (width - height) / 2
   right = (width + height) / 2
   top = 0
   bottom = height
else:
   left = 0
   right = width
   top = (height - width) / 2
   bottom = (height + width) / 2

Step 6:- Crop the image using the above calculated coordinates to achieve a square shape.

img = img.crop((left, top, right, bottom))

Step 7:- Save the square thumbnail image to a file.

img.save('square_thumbnail.jpg')

Step 8:- Display the image using the show() method.

img.show()

Example

# importing the required libraries
from PIL import Image

# open image file
img = Image.open('spiderman.jpg')

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

# crop image to square shape
width, height = img.size
if width > height:
   left = (width - height) / 2
   right = (width + height) / 2
   top = 0
   bottom = height
else:
   left = 0
   right = width
   top = (height - width) / 2
   bottom = (height + width) / 2
img = img.crop((left, top, right, bottom))

# save square thumbnail image
img.save('square_thumbnail.jpg')

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

Output

Method 2- Generating a Circular Thumbnail

In this, we will learn how to create a circular thumbnail, we can first resize the original image, then create a circular mask with the same size as the thumbnail, and paste the resized image onto the mask. For that, we will be using the following algorithm:

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

  • 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.

  • Paste the original image onto the new image using the paste() method giving the circular edge to the new image.

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

Step 1:- Import the necessary modules from the Pillow library: Image and ImageDraw.

from PIL import Image, ImageDraw 

Step 2:- Open the image using the open() method and save it in a variable.

img = Image.open('spiderman.jpg')

Step 3:- Resize the image using the thumbnail() method.

img.thumbnail((2000, 2000))

Step 4:- Create a new grayscale image as the same dimensions of the original image using the Image.new() method.

mask = Image.new('L', img.size, 0)

Step 5:- Create a new object from the mask image using ImageDraw.Draw() method.

draw = ImageDraw.Draw(mask)

Step 6:- Draw an ellipse on the mask image using the draw.ellipse() method. The ellipse should be at the center of the image.

draw.ellipse((0, 0, img.size[0], img.size[1]), fill=255)

Step 7:- Generate a new image from the original image using the Image.new() method.

result = Image.new('RGBA', img.size, (255, 255, 255, 0))

Step 8:- Use the paste() method for pasting the original image onto the new image. Use the mask parameter as the transparency mask to make the edges of the image circular.

result.paste(img, (0, 0), mask)

Step 9:- Save the resulting image as a PNG file using the save() method.

result.save('circular_thumbnail.png')

Step 10:- Display the resulting image using the show() method.

result.show()

Example

# importing the required libraries
from PIL import Image, ImageDraw 

# open image file
img = Image.open('sipderman.jpg')

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

# create circular mask
mask = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, img.size[0], img.size[1]), 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('circular_thumbnail.png')

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

Output

Method 3: Generating a Circular Thumbnail using ImageOps.fit()

In this, we will learn how to create a circular thumbnail using ImageOps. For that, we will be using the following algorithm:

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

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

  • Use the ImageOps.fit() method to resize the image to the desired size.

  • Create a new image for the circular mask using the Image.new() method. Set the mode of the new image to 'L', which stands for grayscale.

  • Create a new ImageDraw.Draw() object and use it to draw an ellipse on the circular mask using the draw.ellipse() method

  • Use the img.putalpha() method to apply the circular mask to the resized image. This method sets the alpha channel of the image to the mask image, which makes all pixels outside the ellipse fully transparent.

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

Step 1:- Import necessary libraries

from PIL import Image, ImageOps, ImageDraw

Step 2:- Open an image file from a file path

img = Image.open('spiderman.jpg')

Step 3:- Resize the image using the ImageOps.fit() method.

img = ImageOps.fit(img, (300, 300))

Step 4:- Create a circular mask of size which you want to preferred and fill it with black colour

mask = Image.new('L', (300, 300), 0)

Step 5:- Create a Draw object for the mask and draw an ellipse with white colour to fill the circular shape

draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, 300, 300), fill=255)

Step 6:- Apply the mask to the original image using the putalpha() method, which sets the alpha channel of each pixel according to the mask.

img.putalpha(mask)

Step 7:- Save the resulting image as a PNG file using the save() method.

img.save('circular_thumbnail.png')

Step 8:- Display the resulting image using the show() method.

img.show()

Example

# importing the required libraries
from PIL import Image, ImageOps, ImageDraw

# open image file
img = Image.open('spiderman.jpg')

# resize image to a square
img = ImageOps.fit(img, (300, 300))

# create circular mask
mask = Image.new('L', (300, 300), 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, 300, 300), fill=255)

# apply mask to image
img.putalpha(mask)

# save circular thumbnail image
img.save('circular_thumbnail.png')

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

Output

Conclusion

In this article, we looked at three different ways to use Pillow to create square or circular thumbnail images. Each strategy has pros and cons, and the optimum strategy will rely on the particular demands of your application. You may quickly create visually appealing thumbnail photos for your web applications, social networking platforms, and photo galleries by employing these strategies.

Updated on: 19-Jul-2023

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements