gallery.py

This is the program I use to generate and maintain my image gallery. It requires Python and the Python Imaging Library.

It seems like everyone's written one of these. This one's mine.

The program generates only static HTML. Thumbnails link directly to their respective pictures, not to another web page with the picture inside. Medium-sized versions of pictures are generated when the original picture is bigger than a certain threshold (see medium_size).

The program reads a file (by default gallery.def) in the current directory. The file may contain variable definitions, image references, links, and comments.

Variable definitions

A variable definition looks like

set name = value

You can set the following variables to alter the behavior of the program:

filename
The name of the output file. By default "index.html".
title
The title of the gallery. By default "Gallery".
table_width
The maximum width in pixels of the table holding the images. By default 700.
cell_width
The width in pixels of each cell in the table. By default 200.
cell_height
The height in pixels of each cell. By default 200.
cell_padding
The padding in pixels around each image. By default 0.
cell_spacing
The spacing in pixels around each cell. By default 5.
medium_size
The maximum size of any side of a medium-sized image. By default 800.
prologue
HTML inserted at the top of the gallery in a little box.
epilogue
HTML inserted at the bottom of the gallery.

Image references

An image reference looks like

image filename description

This causes the program to generate a thumbnail of the image and include it in the output with description. description may be omitted. If filename is image.jpg, the name of the thumbnail file will be image-sm.jpg. If a thumbnail already exists and is the right size, it will not be regenerated.

If any dimension of the image is greater than medium_size, a medium-sized image is generated and linked to instead. The original image is never modified. If filename is image.jpg, the name of the medium-sized image will be image-md.jpg.

If your file name contains spaces, you may surround it with double quotes.

Links

A link looks like

link filename

This will create a link to another gallery defined in filename. The link text will be the title of the linked gallery. The linked gallery will have a link back to the linking gallery.

Linked galleries inherit the values of the linking gallery's variables, except prologue and epilogue.

Comments

A comment looks like

# Comment

The # must be the first non-whitespace character on the line.

Command line

Run gallery.py, giving it the names of your gallery definition files as arguments. If you don't provide any, the program will look for gallery.def.

gallery.py accepts these command-line flags:

-i dir, --input dir
Read input files relative to dir. By default the current directory.
-o dir, --output dir
Write output files to a directory tree rooted at dir. By default the current directory.
-f, --force
Force regeneration of thumbnails and medium-sized images.
-n, --dry-run
Don't actually write any files or create any directories.

Examples

For examples see my image gallery. For every index.html in the directory tree there is a gallery.def that was used to generate it.

Updating a gallery on a remote server is easy with rsync. Use something like rsync -v -r -l -t -e ssh --delete ./ user@host:/var/httpd/images/

Download gallery.py.


Up