Create Thumbnails Programatically

If you have several images following a certain structure on a web page and want them as thumbnails, it can be useful to programatically create these. The manual way of creating thumbnails (using Photoshop or similar) can often be time consuming while the execution time for a script resizing an image is counted in microseconds. If you already have a script this can also be fitted for other similar situations. With the Python Image Library (PIL) thumbnails can be created in no-time. 

In this real word example a Python scripts runs through the folder in which the script is located, resizes each image of the type png and jpg (hopefully the filenames corresponds to the format), when the images have been resized they are put inside a new folder.

Note that PIL ensures a correct aspect ratio and that the size variable just sets the boundary. In the code bellow the important feature is to have images that not exceeds 150px in width (standard convention with width and height). Passing the PIL Image object the size will transform the object into a thumbnail. The method by which the transformation should be performed can be added as an optional parameter. From the documentation: “The filter argument can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS (best quality). If omitted, it defaults to NEAREST.”

#!/usr/bin/python
# -*- coding: utf-8 -*-
import Image
import os
#Imports the Image and os libraries. os is part of standard libraries. Image is part of Python Image Library (PIL)
#PIL can be downloaded from: http://www.pythonware.com/products/pil/

#Creates sets and creates the directory onto where you want your files to be saved
outdir = "150images/"
os.mkdir("./"+outdir)
#If the directory already exists this will cause an OSError.

size = 150, 400 #Set the size that you want to resize your image to.
#Thumbnail automatically checks for ratio consistency so alter the important variable (height or width)

for files in os.listdir("."):
#Sets the appropriate suffix to your files.
outfile = os.path.splitext(files)[0] + "_thumbnail.jpg"
#Transforms JPG formatted files
if files.endswith(".jpg"):
im = Image.open(files)
try:
im.thumbnail(size, Image.ANTIALIAS)
im.save(outdir+outfile, "JPEG")
print "Saved the file: %s" % outdir+outfile
except IOError:
print "cannot create thumbnail for '%s'" % infile
#Transforms PNG formatted files
if files.endswith(".png"):
im = Image.open(files)
try:
im.thumbnail(size, Image.ANTIALIAS)
im.save(outdir+outfile, "JPEG")
print "Saved the file: %s" % outfile
except IOError:
print "cannot create thumbnail for '%s'" % infile

 

 

The script will execute once, but as you execute twice you will get an OSError, this is due to the folder being created in the beginning of the script. I have chosen to name this folder after the significant size boundary in the script (maximum 150px width). PIL is not a part of the standard library, but can easily be installed through pip or easy_install. Python is available on almost every platform and comes preinstalled on the Mac.

Leave a Reply

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