Shrinking Images with Python

Published on Feb. 15, 2008, 6:26 p.m.

My wife had a very large image, and she needed some smaller versions of it. Well, I thought I'd just fire up The GIMP and create a few shrunken versions, but by the time The GIMP loaded on my G4 Mac mini, I'd almost finished writing the python coded needed to do the task for me!

Writing code is good!
import Image
from sys import argv

def shrink(filename):
im =
w,h = im.size

# Create images that are 10%, 20%....90% of the original
sizes = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]

for s in sizes:
nw = int(w*s)
nh = int(h*s)
n = im.resize((nw, nh))
f = "%s_x_%s_%s"%(nw,nh,filename)
print "Saving %s"%(f)

if __name__ == "__main__":

if len(argv) == 2:
print "USAGE: python "

Download this code:
comments powered by Disqus