Help on creating a HTML by python

Walter S. Leipold leipoldw at ace-net.com
Sun Nov 28 23:10:56 EST 2004


evil_pizz at hotmail.com writes:
> Can anyone help me to use a python to create an 
> HTML photo gallery generator.

One way to do this is to download and install ImageMagick from
<imagemagick.org>, then invoke it as an external program from a 
simple Python script.  A sample script that *should* run on 
Windows is appended below.  

> When viewed in a web browser, the HTML file will display the
> thumbnails in a neatly formatted table, and if you click on 
> one of the thumbnails the full-size image will appear.

Of course, this program only creates an HTML file for a single
directory.  You'll have to do additional work if you want this 
process to happen automatically on your Web server -- see Python's
CGI package for details.

Hope this helps...

-- Walt

#!/usr/bin/python
# thumb.py -- Make thumbnails and index page for JPGs.

import os
import os.path
import glob

# ==================

htmlhead = """
<!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>Pictures</TITLE>
</HEAD>
<BODY STYLE="width: 600px">
<P>
"""

htmlfoot = """
</P>
</BODY>
</HTML>
"""

imagerecord = \
    "<A HREF=\"%s\"><IMG BORDER=0 SRC=\"%s\"></A>\n"

# ==================

# ImageMagick command to convert/resize a file.
# (Substitute the path where you've installed ImageMagick.)
convert_cmd = \
    "S:\\programs\\magick\\convert -geometry 160x160 %s %s"

# ==================

# Create directory for thumbnail images if necessary.
thumbdir = "./thumbnails"
if not os.path.exists(thumbdir):
    os.mkdir(thumbdir)

f = open("index.html","w")
f.write(htmlhead)

for fn in glob.glob("*.jpg"):
    print "Processing " + fn + "..."
    thumbfn = os.path.join(thumbdir,fn[:-4] + "_thumb.gif")
    cmd = convert_cmd % (fn,thumbfn)
    rc = os.system(cmd)
    if rc != 0:
        print "Error executing '%s': %d" % (cmd,rc)
        break
    f.write(imagerecord % (fn,thumbfn))

f.write(htmlfoot)
f.close()




More information about the Python-list mailing list