Python - Image Thumbnails



Description

A thumbnail image is a small image file. This thumbnail image is created from the original image.In many cases, thumbnail images are clickable.Instead of downloading actual image, one can have the thumbnail of the image and link the original image.

Syntax

Following is the syntax of functions that are been used.
# Import Pillow:
from PIL import Image,ImageFilter
# Load the original image:
object = Image.open(image)
object.thumbnail(size)
object.save(image)

Parameters

For the thumbnail method from ImageFilter we are going to pass the coordinates in the form of tuple.
x1,y1 - indicates the coordinates Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.

Return value

The new thumbnail image will be saved to the local directory with the save method.

Example

Here is the complete program to crop the image at top left corner and save the image.
import os, sys
from PIL import Image
size = 50, 50
infile = "flower3.jpg"
outfile = os.path.splitext(infile)[0] + "thumbnail.jpg"
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for", infile

Before
flower
After
flower

Advertisements