IAmTheRockstar

Yes. Yes I am.
You are accessing the archives. Commenting has been disabled, and archive posts are likely to be horribly out of date and, in some cases, rendered oddly because the styles from archive posts are no longer being maintained.
April 25, 2007

1 Billion Megapixels? Cue Python Imaging Library

My wife and I were married just over a month ago, and we have some friends who are a wedding photographer/videographer extraordinaire couple. We got our proofs from them on a DVD this week, and I've been struggling to work with them. I don't own one of those fancy schmancy iMacs with 1,182,756,418,237,645,183,645GB of RAM in them, and these pictures are HUMOUNGUS (5MB average). So, obviously, when I get ten of those pictures open, my laptop's system resources take a hit. How 'bout uploading them to my webserver? Fuhget about it! There's a good 375 of them, so it's well over 1GB of data. And why should I make my parents suffer for looking at my wedding photos over the net instead of in person (though they live 20 minutes away...)

As I struggled with dealing with such great Megapixelage, I found a solution: The Python Imaging Library. Unlike some Python libraries, I found that this library had lots of documentation, and I was able to figure out what I wanted to do quickly. For the most part, shrinking the images to 50% was all I really needed to do. So I wrote up this Python script. Although I didn't need to use os.walk, I thought implementing it would make it easier for me to re-use later.

import os, Image
def half( im ):
  newWidth = im.size
  newHeight = im.size
  return im.resize((newWidth, newHeight ) ) #The size needs to pass as a tuple

srcDir = '/path/to/src'
saveDir = '/path/to/dest'
for a, b, c,in os.walk( srcDir ): 
  for image in c:
    imgName = image[1:-4] #trim the extension
    resizeName ='%s/%s_small.jpg' % ( saveDir, imgName )
    im = Image.open( '%s/%s' % ( a, image ) )
    im = half( im )
    im.save( resizedName, 'JPEG' )

Now all my images are half the size. Hooray!

No comments have been posted yet.
Comments are now closed an archived posts.

All opinions expressed here constitute my personal opinion, and do not necessarily represent the opinion of any other organization or person, including, but not limited to, my fellow employees, my employer, its clients or their agents.