Create Image Thumbnails (Python)

vegaseat vegaseat is offline Offline Feb 25th, 2005, 3:08 pm |
0
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.
Quick reply to this message  
Python Syntax
  1. # experiments with the Python Image Library (PIL)
  2. # free from: http://www.pythonware.com/products/pil/index.htm
  3. # create 128x128 (max size) thumbnails of all JPEG images in the working folder
  4. # Python23 tested vegaseat 25feb2005
  5.  
  6. import glob
  7. import Image
  8.  
  9. # get all the jpg files from the current folder
  10. for infile in glob.glob("*.jpg"):
  11. im = Image.open(infile)
  12. # convert to thumbnail image
  13. im.thumbnail((128, 128), Image.ANTIALIAS)
  14. # don't save if thumbnail already exists
  15. if infile[0:2] != "T_":
  16. # prefix thumbnail file with T_
  17. im.save("T_" + infile, "JPEG")
0
vegaseat vegaseat is offline Offline | Mar 20th, 2005
At this point PIL is only available for Python v2.3, sorry! I hope this will change soon.
 
0
vegaseat vegaseat is offline Offline | May 20th, 2005
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
 
0
fernandooa fernandooa is offline Offline | Nov 21st, 2008
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)

  1. import glob
  2. import Image
  3.  
  4. # get all the jpg files from the current folder
  5. for infile in glob.glob("*.jpg"):
  6.  
  7. if infile[0:2] == "T_":
  8. continue # Skipping a thumbnail
  9.  
  10. im = Image.open(infile)
  11.  
  12. # convert to thumbnail image
  13. im.thumbnail((128, 128), Image.ANTIALIAS)
  14. im.save("T_" + infile, "JPEG")
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC