|
|
@ -1,91 +0,0 @@ |
|
|
|
#!/usr/bin/env python3 |
|
|
|
"""html-gallery - generate thumbnails and print html for images |
|
|
|
|
|
|
|
Usage: |
|
|
|
html-gallery [--base_url=<url>] [--base_dir=<dir>] [-x=<pixels>] [-y=<pixels>] [--source=<pattern>...] |
|
|
|
html-gallery (-h | --help) |
|
|
|
html-gallery (-v | --version) |
|
|
|
|
|
|
|
Options: |
|
|
|
-h --help Show this screen. |
|
|
|
--base_url=<url> Base URL for images. |
|
|
|
--base_dir=<dir> Base directory on local filesystem for images and thumbs. |
|
|
|
--source=<pattern> Source directory or image file(s) to make gallery from. |
|
|
|
-x=<pixels> Width of thumbnails |
|
|
|
-y=<pixels> Height of thumbnails |
|
|
|
-v --version Display version |
|
|
|
""" |
|
|
|
|
|
|
|
from docopt import docopt |
|
|
|
from PIL import Image |
|
|
|
import dominate |
|
|
|
import getopt |
|
|
|
from dominate.tags import * |
|
|
|
from resizeimage import resizeimage |
|
|
|
import glob as g, os, sys |
|
|
|
|
|
|
|
def get_images(source): |
|
|
|
images = [] |
|
|
|
|
|
|
|
if os.path.isdir(source): |
|
|
|
# grab things with common file extensions out of dirs |
|
|
|
images.extend(g.glob(source + '/*.JPG')) |
|
|
|
images.extend(g.glob(source + '/*.jpg')) |
|
|
|
images.extend(g.glob(source + '/*.jpeg')) |
|
|
|
images.extend(g.glob(source + '/*.png')) |
|
|
|
images.extend(g.glob(source + '/*.gif')) |
|
|
|
images.extend(g.glob(source + '/*.bmp')) |
|
|
|
else: |
|
|
|
# assume a specific image file - no checking on path |
|
|
|
# because we expect that the user knows they want |
|
|
|
# this image |
|
|
|
images.extend(g.glob(source)) |
|
|
|
|
|
|
|
return images |
|
|
|
|
|
|
|
def main(base_url, base_dir, sources, thumb_x, thumb_y): |
|
|
|
for source in sources: |
|
|
|
# expand sources as necessary - will grab things from a directory, or just |
|
|
|
# return the filename if it's a single file: |
|
|
|
images = get_images(source) |
|
|
|
|
|
|
|
if not images: |
|
|
|
continue |
|
|
|
|
|
|
|
# ensure that thumbnail directory exists |
|
|
|
thumb_directory = base_dir + "/Thumbs" |
|
|
|
os.makedirs(thumb_directory, exist_ok=True) |
|
|
|
|
|
|
|
html_output = '' |
|
|
|
|
|
|
|
for image in images: |
|
|
|
image_basename = os.path.basename(image) |
|
|
|
thumbnail_file = thumb_directory + '/' + image_basename |
|
|
|
thumbnail_src = base_url + 'Thumbs/' + image_basename |
|
|
|
image_href = base_url + image_basename |
|
|
|
html_output += a(img(src=thumbnail_src), href=image_href).render() + "\n" |
|
|
|
|
|
|
|
with open(image, 'r+b') as f: |
|
|
|
with Image.open(f) as source_image: |
|
|
|
thumb = resizeimage.resize_cover(source_image, [thumb_x, thumb_y]) |
|
|
|
thumb.save(thumbnail_file, source_image.format) |
|
|
|
|
|
|
|
print(html_output.strip()) |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
args = docopt(__doc__, version='html-gallery 0.0.1') |
|
|
|
|
|
|
|
# defaults - TODO: make this less shit |
|
|
|
if not args['--base_url']: |
|
|
|
args['--base_url'] = "" |
|
|
|
if not args['--base_dir']: |
|
|
|
args['--base_dir'] = '.' |
|
|
|
if not args['--source']: |
|
|
|
args['--source'] = [ args['--base_dir'] ] |
|
|
|
if not args['-x']: |
|
|
|
args['-x'] = 128 |
|
|
|
if not args['-y']: |
|
|
|
args['-y'] = 128 |
|
|
|
|
|
|
|
main(args['--base_url'], args['--base_dir'], args['--source'], args['-x'], args['-y']) |
|
|
|
sys.exit() |