Python code for organizing photo albums for web presentation
Will Ware
wware at alum.mit.edu
Mon Sep 3 23:04:57 EDT 2001
This code organizes photos from a digital camera for putting
together 56k-modem-friendly web pages using 100x75 thumbnails.
A real-life example is at
http://willware.net:8080/v2k/v2kpix.html
=========================================================
import types, string, os
DEFAULT_CAMERA_FORMAT = "Dsc%05d.jpg"
class Picture:
def __init__(self, picture, text=""):
assert type(text) == types.StringType
self.text = text
if type(picture) == types.StringType:
self.picture = picture
elif type(picture) == types.IntType:
self.picture = DEFAULT_CAMERA_FORMAT % picture
elif isinstance(picture, Picture):
self.picture = picture.picture
else:
raise TypeError, ("what kind of image is this? " +
repr(picture))
self.thumbnailFor = None # reference to larger picture
# from thumbnail
def toHtml(self, partOfPanorama=0):
r = ""
if self.text: r = r + self.text + "<p>\n"
r = r + "<img src=\"" + self.picture + "\">\n"
if (not partOfPanorama):
if self.thumbnailFor != None:
r = (r + "<a href=\"" +
self.thumbnailFor.picture + "\">FULL SIZE</a>\n")
r = r + "<hr>\n"
return r
def thumbnail(self):
if self.thumbnailFor != None:
raise RuntimeError, "this is already a thumbnail"
img = self.picture
n = string.index(img, ".")
teenyName = img[:n] + "TEENY" + img[n:]
# find out if we really need to do the expensive 'convert'
try:
inf = open(teenyName)
inf.close()
except IOError:
cmd = "convert -geometry 100x75 " + img + " " + teenyName
os.system(cmd)
thumbnail = Picture(teenyName, self.text)
thumbnail.thumbnailFor = self
return thumbnail
class Panorama:
def __init__(self, rowscols, piclist, text=""):
assert type(rowscols) == types.TupleType
assert (type(piclist) == types.TupleType or
type(piclist) == types.ListType)
assert type(text) == types.StringType
self.rows, self.cols = R, C = rowscols
assert type(R) == type(C) == types.IntType
assert len(piclist) == R * C
self.text = text
self.piclist = [ ]
for p in piclist:
self.piclist.append(Picture(p, ""))
self.thumbnailFor = None
def toHtml(self):
r = self.text + "<p>\n<table>"
R, C = self.rows, self.cols
for i in range(R):
r = r + "<tr>\n"
for j in range(C):
r = (r + "<td>" +
self.piclist[j + i * C].toHtml(1) +
"</td>\n")
r = r + "</tr>"
r = r + "</table>\n"
if self.thumbnailFor != None:
r = r + ("<a href=\"" + self.thumbnailFor.name +
"\">FULL SIZE</a><hr>\n")
return r
def thumbnail(self):
if self.thumbnailFor != None:
raise RuntimeError, "this is already a thumbnail"
myOldPiclist = self.piclist
img = myOldPiclist[0].picture
n = string.index(img, ".")
bigPanoramaName = img[:n] + "PANORAMA.html"
bigPanorama = Panorama((self.rows, self.cols), myOldPiclist,
self.text)
bigPanorama.name = bigPanoramaName
piclist = [ ]
for p in myOldPiclist:
piclist.append(p.thumbnail())
self.piclist = piclist
self.thumbnailFor = bigPanorama
bigPanoramaAlbum = PhotoAlbum("Another big panorama")
bigPanoramaAlbum.guts.append(bigPanorama)
outf = open(bigPanoramaName, "w")
outf.write(bigPanoramaAlbum.toHtml())
outf.close()
return self
class PhotoAlbum:
def __init__(self, title):
self.title = title
self.guts = [ ]
def toHtml(self):
r = ("<html><title>%s</title>\n" +
"<body bgcolor=\"#FFFFFF\">\n" % self.title)
r = r + "<h1>%s</h1>\n" % self.title
for x in self.guts:
if type(x) == types.StringType:
r = r + x + "\n"
else:
r = r + x.toHtml() + "\n"
return r + "</body></html>\n"
def h1(self, x):
assert type(x) == types.StringType
self.guts.append("<h1>" + x + "</h1>\n")
def h2(self, x):
assert type(x) == types.StringType
self.guts.append("<h2>" + x + "</h2>\n")
def h3(self, x):
assert type(x) == types.StringType
self.guts.append("<h3>" + x + "</h3>\n")
def text(self, x):
assert type(x) == types.StringType
self.guts.append(x)
self.guts.append("<p>\n")
def picture(self, picture, text=""):
self.guts.append(Picture(picture, text))
self.guts.append("<p>\n")
def panorama(self, rowscols, piclist, text=""):
self.guts.append(Panorama(rowscols, piclist, text))
self.guts.append("<p>\n")
def thumbnail(self):
guts = self.guts
for i in range(len(guts)):
x = guts[i]
if type(x) == types.StringType:
pass
elif isinstance(x, Picture):
guts[i] = x.thumbnail()
elif isinstance(x, Panorama):
guts[i] = x.thumbnail()
else:
raise RuntimeError, repr(x)
def main(album, argv):
if len(argv) > 1:
if argv[1] == "-t":
print "<!-- Doing thumbnails -->"
album.thumbnail()
else:
raise RuntimeError, "unknown option: " + argv[1]
print album.toHtml()
================= example usage =========================
#!/usr/bin/python
import photos
Z = photos.PhotoAlbum("Ware's Southwestern USA Vacation, June-July 2000")
Z.h3("Colorado")
Z.picture(2, """Here is one of those automatic watering things that creates
the circular crop fields you see from the airplane window.
The axis of rotation is at the far end.""")
Z.panorama((1,2), (3,6), """Here is the Continental Divide, in
Colorado. This is on the way to Mesa Verde National Park in the
southwestern corner of Colorado.""")
# ... several more photos ....
Z.panorama((1,3), (90, 89, 88), """On to Albequerque, and the National
Atomic Museum at the air force base there. This is a B-52, a huge
friggin behemoth of an airplane.""")
Z.picture(93, """B-52 (vainly): Does my butt look fat to you?""")
if __name__ == "__main__":
import sys
photos.main(Z, sys.argv)
More information about the Python-list
mailing list