Generating a page of thumbnail images
To run the following code, find a folder that contains a bunch of JPG images. For example, you can locate it using pickAFolder(). Having done that, call it from the command area:
dir = pickAFolder()
makeThumbnails(dir)
The thumbnail page is written to the same folder as the JPGs. You can then open it using your favorite browser. Try editing the code and regenerating the page to see the effect of the changes. What happens when you change bgcolor, textcolor, bordercolor, cellpadding, cellspacing or border? What about the image's width and height?
import os
def makeThumbnails(folder):
file = open(folder + os.sep + "thumbnails.html", "wt")
file.write( doctype() )
file.write( head("Thumbnails Page") )
file.write( body(folder) )
file.close()
def doctype():
return """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
"""
def head(str):
return "<html>\n<head>\n<title>" + str + "</title>\n</head>\n"
def body(dir):
bodyHTML = '<body bgcolor="#000000" text="#FFFFFF">\n<h1>Thumbnails</h1>\n'
bodyHTML += "<p>Pictures from " + dir + "</p>\n"
bodyHTML += "<table border='2' cellspacing='0' cellpadding='4' bordercolor='#FFFF00'>\n"
bodyHTML += "<tr><td>Filename</td><td>Image</td></tr>\n"
for file in os.listdir(dir):
if file.endswith(".jpg"):
path = dir + os.sep + file
bodyHTML += '<tr><td>' + file + '</td>'
bodyHTML += '<td><img src="' + path + '" height="100px" width="150px" /></td>'
bodyHTML += '</tr>\n'
bodyHTML += "</table>\n</body>\n</html>"
return bodyHTML
Link to this Page