One more application of the Python Image Library (PIL). This short code creates thumbnails from all JPEG files in the current folder. The thumbnail image file is prefixed with a T_ and saved to the same directory. The program checks for existing files with this prefix, so we don't create T_T_ prefix files and so on.

Edited 6 Years Ago by vegaseat: nice suggestion by fernandooa

# experiments with the Python Image Library (PIL)
# free from:  http://www.pythonware.com/products/pil/index.htm
# create 128x128 (max size) thumbnails of all JPEG images in the working folder
# Python23 tested    vegaseat    25feb2005
# fernandooa modified

import glob
import Image

# get all the jpg files from the current folder
for infile in glob.glob("*.jpg"):
  im = Image.open(infile)
  # don't save if thumbnail already exists
  if infile[0:2] != "T_":
    # convert to thumbnail image
    im.thumbnail((128, 128), Image.ANTIALIAS)
    # prefix thumbnail file with T_
    im.save("T_" + infile, "JPEG")

At this point PIL is only available for Python v2.3, sorry! I hope this will change soon.

Hello.

Excuse me, I'm really new to python (currently learning the library), and I found your blog by looking for a way for creating thumbnails.

Thanks a million for your snippet, just a question (as I'm so new I might have missed something), wouldnt your snippet be more efficient if it were like this? (well, only in case the folder contained any thumnails)

import glob
import Image

# get all the jpg files from the current folder
for infile in glob.glob("*.jpg"):

    if infile[0:2] == "T_":
        continue # Skipping a thumbnail

    im = Image.open(infile)

# convert to thumbnail image
    im.thumbnail((128, 128), Image.ANTIALIAS)
    im.save("T_" + infile, "JPEG")

Of course it would be more efficient. But on the internet you get what you pay for (i.e. a random blog with a random utility, that might not be written too well).

So, according to member sillyman, if you pay nothing you get nothing!

I have news for you, I have paid good money plenty of times and still got nothing!

Further tweakage:

def ThumbnailDir(dir):
  import glob
  import Image


  if not isinstance(dir,type("")): dir="" #i.e. use current directory instead.
  #Optimally, this would raise a type error,
  #but I just ain't fighting with that today.

# get all the jpg files from the selected directory
  for infile in glob.glob(dir+"*.jpg"):

    filename=infile[(infile.rfind("\\")+1):] #Parse escaped string ala "C:\\directory\\subdirectory\\whatever\\"
    #Obviously, switching to "/" would confound this logic.
    if infile[0:2] == "T_":
        continue # Skipping a thumbnail
    try:
     im = Image.open(infile)

# convert to thumbnail image
     im.thumbnail((128, 128), Image.ANTIALIAS)
     im.save(dir + "T_" + filename, "JPEG")
    except IOError:
     #skip the file and continue
     im=None

Still needs work, but this did the job I needed done.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.