#! /usr/bin/env python # $Header: /Users/jch/zStuff/cvsstuff/cvsroot/python/mkthumbs.py,v 1.5 2007/08/04 03:37:35 jch Exp $ # # make thumbnails of all of the .jpg files ina directory # import sys, os import Image import formatter, time, string class DefaultOptions: size = 300 # if true, just print messages about what the script would be doing # but don't actually do anything. noop = 0 # Print lots of messages verbose = 0 # help = 0 # # Some methods # def print_messages(self): return self.verbose or self.noop options = DefaultOptions() even = 0 def create_directory(dir): '''Create the directory dir and its parent directories if necessary''' if os.path.isdir(dir): return parent, base = os.path.split(dir) create_directory(parent) try: if options.print_messages(): print 'create directory %s' % dir if not options.noop: os.mkdir(dir, 0777) except os.error, exc: sys.stderr.write("can't create directory %s:%s\n" % (dir, exc)) def writeEntry(entryname, size): global even print "
\"image
Cap: %s
" % (size[0], size[1], entryname, entryname) even = even + 1 if even > 2: print "" even = 0 def makethumb(jpegfile): if os.path.isfile("./thumbs/"+jpegfile): print "already have thumb for "+jpegfile return try: imfile = Image.open(jpegfile) imfile.thumbnail((options.size, options.size), Image.ANTIALIAS) writeEntry(jpegfile,imfile.size) imfile.save("./thumbs/"+jpegfile) except IOError, iio: sys.stderr.write("can't open image %s reason: %s\n" % (jpegfile,iio)) return except os.error, exc: sys.stderr.write("can't create directory %s:%s\n" % (dir, exc)) def check_option(option, value): if value is None: sys.stderr.write('Value required for option %s\n' % option) sys.exit(1) def parse_cmd_line(): argv = sys.argv[1:] for arg in argv: if '=' in arg: arg, value = string.split(arg, '=', 1) else: value = None if arg in ('-h', '--help'): options.help = 1 elif arg == '--noop': options.noop = 1 elif arg == 'size': options.size = value else: sys.stderr.write('Unknown option %s\n' % arg) def print_header(curdir): print '' print "" print "" print " " print " " print " " print " thumbs for %s" % (curdir) print "" print "" print "

thumbs for %s

" % (curdir) print "
" print "" print "" def print_trailer(): print "" print "
" print "
" print "mkthumps.py " + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(time.time())) print "" print "" def print_help(): dict = opt.__class__.__dict__.copy() dict.update(opt.__dict__) dict['pyversion'] = sys.version[:3] print help_message % dict help_message = """\ usage: mkthumbs.py [options...] Options: -h, --help Print this help message. Do nothing else --noop Do nothing. Just print messages about what would be done. """ def main(): parse_cmd_line() if options.help: print_help() else: create_directory("./thumbs") print_header(os.path.realpath(os.curdir)) files = os.listdir(os.curdir) for infile in files: ext = os.path.splitext(infile)[1] if ext == '.jpg' or ext == '.JPG' or ext == '.gif' or ext == '.png' or ext == '.tiff' or ext == '.jpeg': makethumb(infile) print_trailer() if __name__ == '__main__': main()