Skip to content

Instantly share code, notes, and snippets.

@passiomatic
Created October 4, 2012 12:26
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 passiomatic/3833264 to your computer and use it in GitHub Desktop.
Save passiomatic/3833264 to your computer and use it in GitHub Desktop.
An somewhat old Python script file to create "Ghost Thumbnails" see: http://www.deelan.com/dev/ghost-thumbnails/. Requires Cheetah template engine and Python Imaging Library.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ghost thumbnails</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
#gallery a {background: transparent url(.thumbnails.jpg) top left no-repeat;}
</style>
</head>
<body id="photos-page">
<div id="main">
<div id="content">
<h1>
#raw
Index of <!--#echo var="REQUEST_URI" -->
#end raw
</h1>
<!-- dir listing follows -->
<div id="gallery">
#for index, image in enumerate($image_names)
<a style="background-position: 0 ${index*100*-1}px" href="$image"><span>$image</span></a>
#end for
<div class="cut">&nbsp;</div>
</div>
<!-- /dir listing -->
</div> <!-- / content -->
</div> <!-- / main -->
</body>
</html>
#!/usr/bin/python
from Cheetah.Template import Template
import Image
TEMPLATE_PATH = './index_tmpl.html'
THUMBNAIL_SIZE = 100, 100
THUMBNAILS_NAME = '.thumbnails.jpg'
IMAGE_EXTS = ('.jpg', '.JPG')
# -----------------------------------------------------------------------------
def make_thumbnail(file_dir, filenames):
w, h = THUMBNAIL_SIZE
image_count = len(filenames)
thumbnail_image = Image.new('RGB', (w, image_count * h))
for index, filename in enumerate(filenames):
img = _make_thumbnail(file_dir, filename)
thumbnail_image.paste(img, (0, index * h))
del img
thumbnail_image.save(os.path.join(file_dir, THUMBNAILS_NAME), 'JPEG', quality=70)
del thumbnail_image
def _make_thumbnail(file_dir, filename):
"""
Write an image and its thumbnail to disk.
"""
img = Image.open(os.path.join(file_dir, filename))
width, height = img.size
if width > height:
# landscape
size = THUMBNAIL_SIZE
else:
# portrait
size = THUMBNAIL_SIZE[1], THUMBNAIL_SIZE[0]
# convert to RGB
if img.mode == 'P':
img = img.convert('RGB')
# crop to a square region and make thumbnail out of it
size = min(width, height)
img = img.crop((width/2-size/2, height/2-size/2, width/2+size/2, height/2+size/2))
# thumbnail for the image
img.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
return img
def fill_index_template(template_path, image_names):
from cgi import escape
return Template(file=template_path, searchList=[locals()]).respond()
# -----------------------------------------------------------------------------
import glob
def walk_dir(dir, exts):
image_names = []
for filename in os.listdir(dir):
_, ext = os.path.splitext(filename)
if filename == THUMBNAILS_NAME:
continue # skip
if ext in exts:
image_names.append(filename)
if image_names:
make_thumbnail(dir, image_names)
return image_names
import time, sys, os
def main():
image_names = walk_dir(os.getcwd(), IMAGE_EXTS)
if image_names:
print fill_index_template(TEMPLATE_PATH, image_names)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment