#!/usr/bin/env python2.4 import os, sys import getopt import Image, ImageFile jhead = "/usr/bin/jhead" assert os.access(jhead, os.X_OK), "Can't find jhead executable." def scale(factor, qual, orig, tmp, small, big): # scale orig -> tmp img = Image.open(orig) format = img.format newsize = tuple([int(round(x*factor)) for x in img.size]) img = img.resize(newsize, Image.ANTIALIAS) # save & preserve EXIF img.save(tmp, format, quality=qual) os.system('%s -te "%s" "%s" 2>/dev/null 1>/dev/null' % (jhead, orig, tmp)) # rename to big, small os.rename(orig, big) os.rename(tmp, small) return newsize def main(argv): factor = 0.5 jpegqual = 85 opts, args = getopt.getopt(argv, "f:q:") for o, a in opts: if o=='-f': factor = float(a) elif o=='-q': jpegqual = int(a) for orig in args: base, ext = orig.rsplit(".", 1) tmp = base + "--tmp." + ext if base.endswith("--big"): base = base[:-5] big = orig small = base + "." + ext else: big = base + "--big." + ext small = orig if os.access(big, os.F_OK): print "File already exists: " + big continue #print orig, big newsize = scale(factor, jpegqual, orig, tmp, small, big) print "%s to %ix%i" % (orig, newsize[0], newsize[1]) if __name__=="__main__": main(sys.argv[1:])