944,204 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 20991
  • Python RSS
0

Create Image Thumbnails (Python)

by on Feb 25th, 2005
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.
Last edited by vegaseat; Feb 10th, 2010 at 3:02 pm. Reason: nice suggestion by fernandooa
Python Code Snippet (Toggle Plain Text)
  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. # fernandooa modified
  6.  
  7. import glob
  8. import Image
  9.  
  10. # get all the jpg files from the current folder
  11. for infile in glob.glob("*.jpg"):
  12. im = Image.open(infile)
  13. # don't save if thumbnail already exists
  14. if infile[0:2] != "T_":
  15. # convert to thumbnail image
  16. im.thumbnail((128, 128), Image.ANTIALIAS)
  17. # prefix thumbnail file with T_
  18. im.save("T_" + infile, "JPEG")
Comments on this Code Snippet
Mar 20th, 2005
0

Re: Create Image Thumbnails (Python)

At this point PIL is only available for Python v2.3, sorry! I hope this will change soon.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 20th, 2005
0

Re: Create Image Thumbnails (Python)

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
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 21st, 2008
0

Re: Create Image Thumbnails (Python)

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)

python Syntax (Toggle Plain Text)
  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")
Newbie Poster
fernandooa is offline Offline
1 posts
since Nov 2008
Feb 10th, 2010
0

Re: Create Image Thumbnails (Python)

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).
Newbie Poster
sillyman is offline Offline
1 posts
since Feb 2010
Feb 10th, 2010
0

Re: Create Image Thumbnails (Python)

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!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 27th, 2011
0

Re: Create Image Thumbnails (Python)

Further tweakage:

python Syntax (Toggle Plain Text)
  1. def ThumbnailDir(dir):
  2. import glob
  3. import Image
  4.  
  5.  
  6. if not isinstance(dir,type("")): dir="" #i.e. use current directory instead.
  7. #Optimally, this would raise a type error,
  8. #but I just ain't fighting with that today.
  9.  
  10. # get all the jpg files from the selected directory
  11. for infile in glob.glob(dir+"*.jpg"):
  12.  
  13. filename=infile[(infile.rfind("\\")+1):] #Parse escaped string ala "C:\\directory\\subdirectory\\whatever\\"
  14. #Obviously, switching to "/" would confound this logic.
  15. if infile[0:2] == "T_":
  16. continue # Skipping a thumbnail
  17. try:
  18. im = Image.open(infile)
  19.  
  20. # convert to thumbnail image
  21. im.thumbnail((128, 128), Image.ANTIALIAS)
  22. im.save(dir + "T_" + filename, "JPEG")
  23. except IOError:
  24. #skip the file and continue
  25. im=None

Still needs work, but this did the job I needed done.
Newbie Poster
thenate is offline Offline
1 posts
since Jun 2011
Message:
Previous Thread in Python Forum Timeline: Check if a number is a prime number (Python)
Next Thread in Python Forum Timeline: Py2exe help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC