#!/usr/bin/env python2 #-*-python-*- # # Create a thumbnail page for a directory full of images. # import sys import getopt from PIL import Image import os import ezt thumb_dir = './thumbs/' out_html = 'index.html' N_COL = 8 N_ROW = 4 N_PAGE = N_COL * N_ROW PIXSIZE = 120 def main(title, comment, files): if not os.path.isdir(thumb_dir): os.mkdir(thumb_dir) thumbs = [ ] for infile in files: try: outfile = thumb_dir + os.path.splitext(os.path.basename(infile))[0] \ + '-t.jpg' if os.path.exists(outfile): img = Image.open(infile) size = img.size img = Image.open(outfile) else: img = Image.open(infile) size = img.size img.thumbnail((PIXSIZE, PIXSIZE), Image.ANTIALIAS) img.save(outfile, 'JPEG') print 'created:', outfile image = item() image.width, image.height = size image.twidth, image.theight = img.size image.fname = infile image.thumb = outfile image.fsize = os.path.getsize(infile) / 1024 thumbs.append(image) except KeyboardInterrupt: raise except: t, v, tb = sys.exc_info() ; tb = None print 'EXCEPTION:', t, v print ' Cannot create thumbnail for:', infile template = ezt.Template(os.path.dirname(sys.argv[0]) + '/nailer.ezt') global out_html pageno = 0 totpage = ((len(thumbs) - 1) / N_PAGE) + 1 prev = None while thumbs: pageno = pageno + 1 if len(thumbs) > N_PAGE: next = 'index-%d.html' % pageno else: next = None data = { 'title' : title, 'comment' : comment, 'pageno' : pageno, 'totpage' : totpage, } gen_page(data, template, out_html, thumbs[:N_PAGE], prev, next) del thumbs[:N_PAGE] prev = out_html out_html = 'index-%d.html' % pageno def gen_page(data, template, out_html, thumbs, prev, next): rows = [ ] data = data.copy() data.update({ 'N_COL' : N_COL, 'rows' : rows, 'prev' : prev, 'next' : next, }) for y in range((len(thumbs) + N_COL - 1) / N_COL): one_row = item() one_row.cols = cols = [ ] rows.append(one_row) for x in range(N_COL): i = y*N_COL + x if i >= len(thumbs): break cols.append(thumbs[i]) template.generate(open(out_html, 'w'), data) class item: pass if __name__ == '__main__': title = 'Picture Index' comment = None opts, args = getopt.getopt(sys.argv[1:], 't:c:') for opt, val in opts: if opt == '-t': title = val elif opt == '-c': comment = val main(title, comment, args)