pythumb Thumbnail Image Creator
pythumb is an thumbnail image creator developed in Python.
We recently needed a thumbnail creator that's capable or recursive directory scanning, run cross platform, easy to use and free. And there came the pythumb.
The code is not entirely polished, but did serve its purpose for us.
- Installing Python Imaging on Windows is pretty straight forward. Grap the binary distro from pythonware.com and run the installer.
- On Mac environments, grab the package from effbot.org/downloads/. Unzip the contents. Take a look at the README file provided with the package. If your environment complies with the requirements, the following command would do the trick.
$ python setup.py install
Requirements
- Python 2.5 (should work for Python 2.2 and up)
- Python Imaging Library
Once you have those, you're good to go.
Usage
pythumb runs on the command line arguments provided. The arguments are self-explanatory, so won't be providing details on those.
Type the following in the command line to see the arguments required.
$ python pythumb.py
Usage: pythumb.py [-d] directory [-r] recursive [-u] uniform_size [-w] width [-h] height
[-f] filter [-p] prefix [-s] suffix [-i] image_resize_filter [-o] output_directory
By Brilaps LLC, http://brilaps.com, (c) 2008
Options:
--version show program version number and exit
-d DIRECTORY, --directory=DIRECTORY
The top directory for the images to be thumbed down
:).
-f FILE_FILTER, --file_filter=FILE_FILTER
File search wildcard. *.jpg, *.png etc.
-r, --recursive Run a recursive scan starting from the given top
folder.
-w WIDTH, --width=WIDTH
Width of the thumbnail image. Aspect ratio of the
image will be preserved
-h HEIGHT, --height=HEIGHT
Height of the thumbnail image. Aspect ratio of the
image will be preserved
-i IMAGE_RESIZE_FILTER, --image_resize_filter=IMAGE_RESIZE_FILTER
The filter argument can be one of NEAREST, BILINEAR,
BICUBIC, or ANTIALIAS (best quality).
-p PREFIX, --prefix=PREFIX
Prefix to be added to the created thumbnail.
-s SUFFIX, --suffix=SUFFIX
Suffix to be added to the created thumbnail.
-u, --uniform_size Makes sure that the thumbnails are same size (also
meaning, the image needs to be temporarily resized to
a square based on the max dimension)
-o OUTPUT_DIRECTORY, --output_directory=OUTPUT_DIRECTORY
The output directory where you want to store the
created thumbnails.
[-f] filter [-p] prefix [-s] suffix [-i] image_resize_filter [-o] output_directory
By Brilaps LLC, http://brilaps.com, (c) 2008
Options:
--version show program version number and exit
-d DIRECTORY, --directory=DIRECTORY
The top directory for the images to be thumbed down
:).
-f FILE_FILTER, --file_filter=FILE_FILTER
File search wildcard. *.jpg, *.png etc.
-r, --recursive Run a recursive scan starting from the given top
folder.
-w WIDTH, --width=WIDTH
Width of the thumbnail image. Aspect ratio of the
image will be preserved
-h HEIGHT, --height=HEIGHT
Height of the thumbnail image. Aspect ratio of the
image will be preserved
-i IMAGE_RESIZE_FILTER, --image_resize_filter=IMAGE_RESIZE_FILTER
The filter argument can be one of NEAREST, BILINEAR,
BICUBIC, or ANTIALIAS (best quality).
-p PREFIX, --prefix=PREFIX
Prefix to be added to the created thumbnail.
-s SUFFIX, --suffix=SUFFIX
Suffix to be added to the created thumbnail.
-u, --uniform_size Makes sure that the thumbnails are same size (also
meaning, the image needs to be temporarily resized to
a square based on the max dimension)
-o OUTPUT_DIRECTORY, --output_directory=OUTPUT_DIRECTORY
The output directory where you want to store the
created thumbnails.
Demo
You just browse our products pages for pythumb-created thumbnails. *see the lighbox thumbnails at the bottom of most of the pages
Code
And here comes the code:
##############################################
## pythumb
## version 0.8
## Thumbnail Image Creator
##
## Copyright 2008, Brilaps LLC
##
## @license The MIT License (http://www.opensource.org/licenses/mit-license.php)
##
## You will need Python Imaging Library http://www.pythonware.com/products/pil/
##############################################
import os
import sys
import glob
import string
import Image
import fnmatch
from os.path import join
from optparse import OptionParser
#some defaults
directory = "./"
file_filter = "*.jpg"
width = 150
height = 150
image_resize_filter = "ANTIALIAS"
prefix = "small_"
suffix = ""
recursive = False
output_directory = "./"
uniform_size = True
def get_files(path, filter, recursive):
matched = list()
if (recursive == True):
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name,filter):
matched.append(join(root, name));
else:
for file in glob.glob1(path, filter):
matched.append(join(path, file))
return matched
def usage():
global directory
global file_filter
global width
global height
global image_resize_filter
global prefix
global suffix
global recursive
global output_directory
global uniform_size
usage = "usage: %prog [-d] directory [-r] recursive [-u] uniform_size [-w] width [-h] height [-f] filter [-p] prefix [-s] suffix [-i] image_resize_filter [-o] output_directory "
parser = OptionParser(usage=usage, version="pythumb 0.8", add_help_option=False)
parser.set_description("By Brilaps LLC, http://brilaps.com, (c) 2008")
parser.add_option("-d", "--directory", action="store", type="string", dest="directory",
help="The top directory for the images to be thumbed down :).")
parser.add_option("-f", "--file_filter", action="store", type="string", dest="file_filter",
help="File search wildcard. *.jpg, *.png etc.")
parser.add_option("-r", "--recursive", action="store_true", dest="recursive",
help="Run a recursive scan starting from the given top folder.")
parser.add_option("-w", "--width", action="store", type="int", dest="width",
help="Width of the thumbnail image. Aspect ratio of the image will be preserved")
parser.add_option("-h", "--height", action="store", type="int", dest="height",
help="Height of the thumbnail image. Aspect ratio of the image will be preserved")
parser.add_option("-i", "--image_resize_filter", action="store", type="string", dest="image_resize_filter",
help="The filter argument can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS (best quality).")
parser.add_option("-p", "--prefix", action="store", type="string", dest="prefix",
help="Prefix to be added to the created thumbnail.")
parser.add_option("-s", "--suffix", action="store", type="string", dest="suffix",
help="Suffix to be added to the created thumbnail.")
parser.add_option("-u", "--uniform_size", action="store_true", dest="uniform_size",
help="Makes sure that the thumbnails are same size (also meaning, the image needs to be temporarily resized to a square based on the max dimension)")
parser.add_option("-o", "--output_directory", action="store", type="string", dest="output_directory",
help="The output directory where you want to store the created thumbnails.")
(options, args) = parser.parse_args()
parser.print_help()
if (options.directory != None):
directory = options.directory
if (options.file_filter != None):
file_filter = options.file_filter
if (options.recursive != None):
recursive = options.recursive
if (options.width != None):
width = options.width
if (options.height != None):
height = options.height
if (options.image_resize_filter != None):
image_resize_filter = options.image_resize_filter
if (options.prefix != None):
prefix = options.prefix
if (options.suffix != None):
suffix = options.suffix
if (options.uniform_size != None):
uniform_size = options.uniform_size
if (options.output_directory != None):
output_directory = options.output_directory
##print what we're about to do...
print ("\nCreating thumbnails of %s files in %s into %s\n\tuniform size: %d, recursive: %d, filter: %s, prefix: %s, suffix: %s\n\twidth: %d, height:%d\n"
% (file_filter, directory, output_directory, uniform_size, recursive, image_resize_filter, prefix, suffix, width, height) )
return
def pythumbitize():
global directory
global file_filter
global width
global height
global image_resize_filter
global prefix
global suffix
global recursive
global output_directory
global uniform_size
resize_filter = {
'ANTIALIAS': Image.ANTIALIAS,
'BICUBIC': Image.BICUBIC,
'BILINEAR': Image.BILINEAR,
'NEAREST': Image.NEAREST,
}[image_resize_filter]
files = list(get_files(directory, file_filter, recursive))
for file in files:
img = Image.open(file).convert("RGB")
#need to make a square image based on the max dimension of the actual image
if (uniform_size == True):
tempimage = img.copy()
(actualwidth, actualheight) = tempimage.size
img = tempimage.resize((max(actualwidth, actualheight), max(actualwidth, actualheight)), resize_filter)
img.thumbnail((width, height), resize_filter)
(path, filename) = os.path.split(file)
(basefilename, extension) = os.path.splitext( filename )
#if the initial output directory is the same as the input, put the thumbnails into the same subfolders as the actual images
if (output_directory == directory):
thumbpath = path
else:
thumbpath = output_directory
thumbnail = join(thumbpath, prefix + basefilename + suffix + extension)
print ("creating %s\n" % thumbnail)
try:
img.save(thumbnail)
except:
print ("error creating %s\n" % thumbnail)
for error in sys.exc_info():
print "\t%s\n" % error
return
if __name__ == '__main__':
usage()
pythumbitize()
## pythumb
## version 0.8
## Thumbnail Image Creator
##
## Copyright 2008, Brilaps LLC
##
## @license The MIT License (http://www.opensource.org/licenses/mit-license.php)
##
## You will need Python Imaging Library http://www.pythonware.com/products/pil/
##############################################
import os
import sys
import glob
import string
import Image
import fnmatch
from os.path import join
from optparse import OptionParser
#some defaults
directory = "./"
file_filter = "*.jpg"
width = 150
height = 150
image_resize_filter = "ANTIALIAS"
prefix = "small_"
suffix = ""
recursive = False
output_directory = "./"
uniform_size = True
def get_files(path, filter, recursive):
matched = list()
if (recursive == True):
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name,filter):
matched.append(join(root, name));
else:
for file in glob.glob1(path, filter):
matched.append(join(path, file))
return matched
def usage():
global directory
global file_filter
global width
global height
global image_resize_filter
global prefix
global suffix
global recursive
global output_directory
global uniform_size
usage = "usage: %prog [-d] directory [-r] recursive [-u] uniform_size [-w] width [-h] height [-f] filter [-p] prefix [-s] suffix [-i] image_resize_filter [-o] output_directory "
parser = OptionParser(usage=usage, version="pythumb 0.8", add_help_option=False)
parser.set_description("By Brilaps LLC, http://brilaps.com, (c) 2008")
parser.add_option("-d", "--directory", action="store", type="string", dest="directory",
help="The top directory for the images to be thumbed down :).")
parser.add_option("-f", "--file_filter", action="store", type="string", dest="file_filter",
help="File search wildcard. *.jpg, *.png etc.")
parser.add_option("-r", "--recursive", action="store_true", dest="recursive",
help="Run a recursive scan starting from the given top folder.")
parser.add_option("-w", "--width", action="store", type="int", dest="width",
help="Width of the thumbnail image. Aspect ratio of the image will be preserved")
parser.add_option("-h", "--height", action="store", type="int", dest="height",
help="Height of the thumbnail image. Aspect ratio of the image will be preserved")
parser.add_option("-i", "--image_resize_filter", action="store", type="string", dest="image_resize_filter",
help="The filter argument can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS (best quality).")
parser.add_option("-p", "--prefix", action="store", type="string", dest="prefix",
help="Prefix to be added to the created thumbnail.")
parser.add_option("-s", "--suffix", action="store", type="string", dest="suffix",
help="Suffix to be added to the created thumbnail.")
parser.add_option("-u", "--uniform_size", action="store_true", dest="uniform_size",
help="Makes sure that the thumbnails are same size (also meaning, the image needs to be temporarily resized to a square based on the max dimension)")
parser.add_option("-o", "--output_directory", action="store", type="string", dest="output_directory",
help="The output directory where you want to store the created thumbnails.")
(options, args) = parser.parse_args()
parser.print_help()
if (options.directory != None):
directory = options.directory
if (options.file_filter != None):
file_filter = options.file_filter
if (options.recursive != None):
recursive = options.recursive
if (options.width != None):
width = options.width
if (options.height != None):
height = options.height
if (options.image_resize_filter != None):
image_resize_filter = options.image_resize_filter
if (options.prefix != None):
prefix = options.prefix
if (options.suffix != None):
suffix = options.suffix
if (options.uniform_size != None):
uniform_size = options.uniform_size
if (options.output_directory != None):
output_directory = options.output_directory
##print what we're about to do...
print ("\nCreating thumbnails of %s files in %s into %s\n\tuniform size: %d, recursive: %d, filter: %s, prefix: %s, suffix: %s\n\twidth: %d, height:%d\n"
% (file_filter, directory, output_directory, uniform_size, recursive, image_resize_filter, prefix, suffix, width, height) )
return
def pythumbitize():
global directory
global file_filter
global width
global height
global image_resize_filter
global prefix
global suffix
global recursive
global output_directory
global uniform_size
resize_filter = {
'ANTIALIAS': Image.ANTIALIAS,
'BICUBIC': Image.BICUBIC,
'BILINEAR': Image.BILINEAR,
'NEAREST': Image.NEAREST,
}[image_resize_filter]
files = list(get_files(directory, file_filter, recursive))
for file in files:
img = Image.open(file).convert("RGB")
#need to make a square image based on the max dimension of the actual image
if (uniform_size == True):
tempimage = img.copy()
(actualwidth, actualheight) = tempimage.size
img = tempimage.resize((max(actualwidth, actualheight), max(actualwidth, actualheight)), resize_filter)
img.thumbnail((width, height), resize_filter)
(path, filename) = os.path.split(file)
(basefilename, extension) = os.path.splitext( filename )
#if the initial output directory is the same as the input, put the thumbnails into the same subfolders as the actual images
if (output_directory == directory):
thumbpath = path
else:
thumbpath = output_directory
thumbnail = join(thumbpath, prefix + basefilename + suffix + extension)
print ("creating %s\n" % thumbnail)
try:
img.save(thumbnail)
except:
print ("error creating %s\n" % thumbnail)
for error in sys.exc_info():
print "\t%s\n" % error
return
if __name__ == '__main__':
usage()
pythumbitize()
Enjoy
Support
Forum is a good place to start a support request, or click here to send us an email.
Products
CategoryWiki
There are 3 comments on this page. [Show comments]
