#!/usr/local/bin/python ########################################################### ## albumize.py ## Web album builder ## ## Version 0.8, 01-Jan-2003 ## ## About: ## Creates a simple web album for all images in a ## directory. The album consists of one main webpage ## containing clickable thumnails and one webpage for ## each image containing 'next', 'prev' and 'up' links. ## A subdirectory for thumnails is created and all images ## are scaled down and copied there. ## ## Requirements: ## Python 2.2 ## Python Imaging Library 1.1.3 ## Tested on FreeBSD 4.7 but expected to work on any ## Unixish or Windowie machine. ## ## Usage: ## Syntax: ## $ python albumize.py directory album_name ## Example: ## $ python albumize.py ~/sanfran_photos "San Fran" ## May want to edit constants in code below, specially ## COPYRIGHT ## ## Todo: ## o Allow resizing images for album and create album in ## 'albumized' subdirectory ## o Be smart on incremental runs (avoid rebuilding ## thumbnails etc) ## o Use exif to extract datetime (possibly other info) ## and embed in image page ## o Use exif to extract all info and create 'info' page ## for each image linked from image page ## o Command linify constants ## o Design feature to add comment to image pages and ## album main page ## ## Copyright (c) 2003, Shalabh Chaturvedi ## ## Permission is hereby granted, free of charge, to any ## person obtaining a copy of this software and associated ## documentation files (the "Software"), to deal in the ## Software without restriction, including without ## limitation the rights to use, copy, modify, merge, ## publish, distribute, sublicense, and/or sell copies of ## the Software, and to permit persons to whom the Software ## is furnished to do so, subject to the following ## conditions: ## ## The above copyright notice and this permission notice ## shall be included in all copies or substantial portions ## of the Software. ## ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY ## KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ## THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A ## PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, ## DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF ## CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN ## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS ## IN THE SOFTWARE. ## ########################################################### import sys, os, string try: import Image except ImportError: print 'Could not import Image - ' \ 'are you sure Python Imaging Library is installed?' sys.exit(2) EXTENSIONS = ['jpg', 'png', 'gif'] THUMBSIZE = (80, 60) THUMBSDIRNAME = 'thumbs' INDEXHTML = 'index.html' COPYRIGHT = '© 2002, Shalabh Chaturvedi' extensions = EXTENSIONS + [string.upper(x) for x in EXTENSIONS] copyright = COPYRIGHT and '''
%s
''' % COPYRIGHT def albumize(path, album_name): path = os.path.abspath(path) print "Albumizing '%s'" % path # verify thumbs directory print 'Verifying thumbs directory...' thumbspath = os.path.join(path, THUMBSDIRNAME) if os.path.exists(thumbspath): assert os.path.isdir(thumbspath), \ "File in way of creating thumbnail directory '%s'" % thumbspath else: os.mkdir(thumbspath) files = os.listdir(path) files = [f for f in files if f[-3:] in extensions] # Lowercase the filenames, this isn't essential. Also, we use this # seemingly strange copy to rename because this is the only way it # works with fat filesystems. Safely comment-outable all the way # upto 'end lowercasing code'. print 'Lowercasing filenames...', i = 0 for filename in files: newname = filename.lower() if newname!=filename: filename = os.path.join(path, filename) newname = os.path.join(path, newname) d = file(filename, 'r').read() os.unlink(filename) f = file(newname, 'w') f.write(d) f.close() i += 1 files = [f.lower() for f in files] print '%s files renamed' % i # end lowercasing code files.sort() print 'Building pages and thumbnails...' # build the pages, and thumbnails thumbs = [] numfiles = len(files) for i in xrange(numfiles): # build the page prevlink = i!=0 and link(pagename(files[i-1]), 'Back <<') or 'Start' nextlink = i>') or 'End' uplink = link(INDEXHTML, 'Up') pagetext = page(imgname=files[i], prevlink=prevlink, nextlink=nextlink, uplink=uplink, title=album_name, position='%s of %s' % (i+1, numfiles), copyright=copyright) pgname = pagename(files[i]) f = open(os.path.join(path, pgname), 'w') f.write(pagetext) f.close() # build the thumbnail img = Image.open(os.path.join(path, files[i])) img = img.resize(THUMBSIZE) thname = thumbname(files[i]) img.save(os.path.join(thumbspath, thname)) thumbs.append( (os.path.join(THUMBSDIRNAME, thname), pgname) ) # build index file f = open(os.path.join(path, INDEXHTML), 'w') txt = indexpage(album_name, thumbs) f.write(txt) f.close() print 'Done (%s images)' % numfiles def thumbname(imgname): return imgname + '_thumb.jpg' def pagename(imgname): return imgname + '.html' def link(target, name): return '%s' % (target, name) def indexpage(title, thumbs): t = [] for img,pagelink in thumbs: t.append(link(pagelink, '' % img)) t = '\n'.join(t) t = ''' %s

%s

%s
%s ''' % (title, title, t, copyright) return t def page(**kw): t = ''' %(title)s

%(title)s

%(uplink)s

%(prevlink)s

%(nextlink)s
%(position)s

%(copyright)s ''' % kw return t if __name__=='__main__': try: path = sys.argv[1] album_name = sys.argv[2] except IndexError: print ''' Syntax: albumize path album_name Example: albumize ~/sanfran_photos "San Francisco Trip"''' sys.exit(1) albumize(path, album_name)