Skip to content

Instantly share code, notes, and snippets.

@pyx
Created October 8, 2012 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyx/3851500 to your computer and use it in GitHub Desktop.
Save pyx/3851500 to your computer and use it in GitHub Desktop.
poor man's photo album
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from os.path import abspath, expanduser, join, splitext, relpath
IMG = ".jpg .jpeg .png .gif".split()
HEADER = r"""<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<style type="text/css">
{css}
</style>
</head>
<body>
"""
FOOTER = r"""
</body>
</html>
"""
CSS = r"""
img {
width: 30%;
border: 1px lavender solid;
}
"""
HEADLINE = '<h1>{0}</h1>'
DIR = '<h2><a href="{href}">{dirname}</a></h2>'
HR = '<hr />'
A_TAG = '<a href="{href}" title="{title}"><img src="{src}" alt="{alt}" /></a>'
def gen_html(root=None, absolute_path=True):
cwd = os.getcwd()
root = expanduser(root) if root else cwd
path_func = abspath if absolute_path else lambda p: relpath(p, cwd)
title = 'All images in ' + root
print(HEADER.format(title=title, css=CSS))
print(HEADLINE.format(title))
for dirpath, dirnames, filenames in os.walk(root):
path = path_func(dirpath)
print(HR)
print(DIR.format(href=path, dirname=path))
for file in filenames:
name, ext = os.path.splitext(file)
if ext in IMG:
path = path_func(join(dirpath, file))
print(A_TAG.format(href=path, title=path, src=path, alt=name))
print(FOOTER)
if __name__ == '__main__':
gen_html()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment