When you create a python-based application or website, you may need to display thumbnails of images on it. But the original images available to us may not be of the required size and aspect ratio. In such cases, you will need to create thumbnail from image in Python. In this article, we will learn how to create thumbnail from image in Python.
How to Create Thumbnail from Image in Python
Here are the steps to create thumbnail from image in python. We will use PIL module for this purpose.
1. Install PIL
Python Image Library (PIL) is a useful library that provides tons of functions for image editing. You can also use Pillow library for this purpose, which is basically a fork of PIL.
Open terminal and run the following command to install PIL in Linux.
$ sudo pip install pil OR $ sudo pip install pillow
2. Create Thumbnail from Image
PIL already provides thumbnail() function to create thumbnails from image files. It will modify the input image’s size and keep it limited to the specified size. It also preserves the aspect ratio of original image, where applicable. Here is the syntax of thumbnail() function.
Here is what the input arguments mean.
- Size − Required size
- Resample − Optional resampling filter. It can be one of these PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, or PIL.Image.LANCZOS. If omitted, it defaults to PIL.Image.BICUBIC.
Here is a sample function to convert images.
from PIL import Image def thumbnails(): try: image = Image.open('images/cat.jpg') image.thumbnail((100,90)) image.save('images/thumbnail.jpg') image1 = Image.open('images/thumbnail.jpg') image1.show() except IOError: pass thumbnails()
In the above code, we first import PIL library. Then we create thumbnails() function, where we use open() function to open the image file, which returns an object. We call thumbnail() function on this image object and input thumbnail size of 100px x 90px. Next, we call save() function to save the image as thumbnail.jpg. If you don’t specify the output filename in save() function, the original file will be overwritten.
Finally, we open the thumbnail file to view it.
In this article, we have learnt how to create thumbnail from images using PIL/Pillow library. You can us the above code on all image formats such as JPG, PNG, etc.
Thumbnails are often required in applications and websites, especially on ecommerce website to display product thumbnails or social media websites to display user profile thumbnails. In all these use cases, you can refer to the above mentioned code to create thumbnail of images. Some developers retain the original images and directly render them as thumbnails in websites. In this case, original images take up a lot more space than thumbnails. On the other hand, if you convert images to thumbnails, they will take up less space and also improve speed of your application/website. In most cases, thumbnail creation is part of a bigger module or function that also does other things. So you can include the above code into your application, as per your requirement.