Python code for organizing/web-presenting photo albums

Will Ware wware at alum.mit.edu
Mon Sep 3 23:10:01 EDT 2001


###################################################################
#
# This is some Python code for organizing photos from digital cameras
# for presentation on the web. To be friendly to non-cable-modems, we
# use 100x75 thumbnails. The camera takes 640x480 pictures. A real
# life example is at
# http://willware.net:8080/v2k/v2kpix.html
#
######################### photos.py ###############################

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   # referent 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' 
operation
         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 pictures and panoramas ...

if __name__ == "__main__":
     import sys
     photos.main(Z, sys.argv)




More information about the Python-list mailing list