#! /usr/bin/env python # coding: utf-8 usage = """Usage: ./make_individual_html.py image_sizes descrip Examines the descrip file and builds a medium, large and full size HTML page for each image, with forward, up, and back links on each page. image_sizes looks like: IMG_0256.jpg 2048 1536 IMG_0256_lg.jpg 1200 900 IMG_0256_med.jpg 800 600 IMG_0256_thumb.jpg 200 150 descrip looks like: 100_2455 h Driving down Miller Hill 100_2549 v Storage shed next to the red cabin This script Copyright © 2010, Christopher Swingley, and licensed under the terms of the GNU General Public License v3.0. Use at your own risk. """ import sys if len(sys.argv) != 3: print "Error: Two arguments (image_sizes file, descrip file) required" print print usage sys.exit(1) # Read all the image sizes image_sizes = {} with open(sys.argv[1]) as f: for line in f.readlines(): (img, width, height) = line.split() (width, height) = map(int, (width, height)) image_sizes[img] = (width, height) data = [] with open(sys.argv[2]) as f: for line in f.readlines(): line = line.strip() fields = line.split() image_base, orientation = fields[:2] descrip = ' '.join(fields[2:]) data.append((image_base, orientation, descrip)) def build_link_string(i, size_str): if size_str == 'full': if i == 0: links = '|| ^^ >>' % (data[i+1][0]) elif i == len(data) - 1: links = '<< ^^ ||' % (data[i-1][0]) else: links = '<< ^^ >>' % (data[i-1][0], data[i+1][0]) else: if i == 0: links = '|| ^^ >>' % (data[i+1][0], size_str) elif i == len(data) - 1: links = '<< ^^ ||' % (data[i-1][0], size_str) else: links = '<< ^^ >>' % (data[i-1][0], size_str, data[i+1][0], size_str) return links def build_image_string(image_base, orientation, size_str): if size_str == 'full': image_size_str = 'width="%s" height="%s"' % (image_sizes["%s.jpg" % (image_base)]) image_string = '' % (image_base, image_size_str) else: image_size_str = 'width="%s" height="%s"' % (image_sizes["%s_%s.jpg" % (image_base, size_str)]) image_string = '' % (image_base, size_str, image_size_str) return image_string for i in range(len(data)): # for i in range(2): html_header_string = '\n\n\n\n\n\nCabin renovation, Summer 2010' (image_base, orientation, descrip) = data[i] print "Making %s_med.html" % (image_base) ofl = open("%s_med.html" % (image_base), 'w') links = build_link_string(i, "med") image_string = build_image_string(image_base, orientation, "med") ofl.write('%s

%s

%s
%s
%s

' % (html_header_string, descrip, links, image_string, links)) ofl.close() print "Making %s_lg.html" % (image_base) ofl = open("%s_lg.html" % (image_base), 'w') links = build_link_string(i, "lg") image_string = build_image_string(image_base, orientation, "lg") ofl.write('%s

%s

%s
%s
%s

' % (html_header_string, descrip, links, image_string, links)) ofl.close() print "Making %s.html" % (image_base) ofl = open("%s.html" % (image_base), 'w') links = build_link_string(i, "full") image_string = build_image_string(image_base, orientation, "full") ofl.write('%s

%s

%s
%s
%s

' % (html_header_string, descrip, links, image_string, links)) ofl.close()