Create Image Thumbnails (Python)

Updated vegaseat 1 Tallied Votes 2K Views Share

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.

# 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")
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The Python Image Library (PIL) is now available for Python24.
Download and instal
PIL-1.1.5.win32-py2.4.exe [533k]
from
http://effbot.org/downloads/#imaging

fernandooa 0 Newbie Poster

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")
sillyman 0 Newbie Poster

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).

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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!

thenate 0 Newbie Poster

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.