954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Create Image Thumbnails (Python)

0
By vegaseat on Feb 26th, 2005 1:08 am

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

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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")
fernandooa
Newbie Poster
1 post since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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

sillyman
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

thenate
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You