Created
June 25, 2014 18:26
-
-
Save acdha/0e03765c927df81edcb8 to your computer and use it in GitHub Desktop.
Quick and dirty HTML index creation for image galleries (see e.g. http://chris.improbable.org/experiments/opencv/page-image-extraction/211/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from cgi import escape | |
from urllib import quote | |
import os | |
import sys | |
import re | |
TEMPLATE_HEADER = u"""<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Extracted Images</title> | |
<style type="text/css"> | |
html { | |
width: 100vw; | |
max-width: 100vw; | |
font-family: sans-serif; | |
} | |
div > a { | |
display: inline-block; | |
margin: 10px; | |
vertical-align: top; | |
} | |
div > a > img { | |
max-width: 384px; | |
max-height: 384px; | |
border: solid blue 2px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Extracted Images</h1> | |
<div id="gallery"> | |
""" | |
TEMPLATE_FOOTER = """ | |
</div> | |
</body> | |
</html> | |
""" | |
with open("index.html", "wb") as output: | |
print >>output, TEMPLATE_HEADER | |
for f in sorted(os.listdir(".")): | |
if not f.endswith(".jpg"): | |
continue | |
m = re.match(r'^(\d+)_(\d+)_(\d+) extract (\d+)[.]jpg$', f) | |
if not m: | |
print >>sys.stderr, "Ignoring %s" % f | |
continue | |
groups = m.groups() | |
url = 'http://www.wdl.org/en/item/{0}/view/{1}/{2}/'.format(*groups) | |
label = 'Extract #{3} from volume {1} page {2}' | |
print >>output, """<a href="{url}"><img alt="{label}" src="{filename}"></a>""".format(filename=quote(f.encode("utf-8")), | |
url=url, | |
label=escape(label)) | |
print >>output, TEMPLATE_FOOTER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment