From b613eef36c7c3a0d2b95607c6ee579b98c991fa5 Mon Sep 17 00:00:00 2001 From: rra Date: Wed, 4 Sep 2019 16:13:29 +0200 Subject: [PATCH] speed up by not calling exiftools always, images now always have figcaptions, catch errors properly --- distribusi/distribusi.py | 66 ++++++++++++++++++---------------------- setup.py | 2 -- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index 334f26f..eba9312 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -7,27 +7,18 @@ import magic from PIL import Image from distribusi.page_template import html_footer, html_head - -CODE_TYPES = ['x-c', 'html'] - -FILE_TYPES = { - 'image': '
{}
', - 'pdf': ( - '' - '' - ), - 'text': '{}', - 'video': (''), - 'audio': (''), -} - +from distribusi.mappings import CODE_TYPES, FILE_TYPES MIME_TYPE = magic.Magic(mime=True) def caption(image): - process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE) - out, err = process.communicate() + try: + process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE) + out, err = process.communicate() + except Exception as e: + print(e) + print('Do you have exiftool installed?') try: caption = out.decode("utf-8").split(": ", 1)[1] except: @@ -36,21 +27,25 @@ def caption(image): def thumbnail(image, name, args): - size = (450, 450) - im = Image.open(image) - im.thumbnail(size) - output = BytesIO() - im.save(output, format='JPEG') - im_data = output.getvalue() - data_url = base64.b64encode(im_data).decode() - cap = caption(image) - if cap and args.captions: - cap = "
{}
".format(cap) - else: - cap = '' - return ( - "
{}
" - ).format(name, data_url, cap) + try: + size = (450, 450) + im = Image.open(image) + im.thumbnail(size) + output = BytesIO() + im.save(output, format='JPEG') + im_data = output.getvalue() + data_url = base64.b64encode(im_data).decode() + if args.captions: + cap = caption(image) + else: + cap = name + return ( + "
{}
" + ).format(name, data_url, cap) + except Exception as e: + print(e) + return "
{}
".format(name, name,name) + def div(args, mime, tag, *values): @@ -60,7 +55,7 @@ def div(args, mime, tag, *values): else: filename = '' if 'image' in mime: - html = '
{}' + filename + '
' + html = '
{}
' elif 'pdf' in mime: html = '
{}' + filename + '
' else: @@ -103,11 +98,10 @@ def distribusify(args, directory): # noqa if mime == 'image' and args.thumbnail: a = thumbnail(full_path, name, args) else: - cap = caption(full_path) - if cap and args.captions: - cap = "
{}
".format(cap) + if args.captions: + cap = caption(full_path) else: - cap = '' + cap = name a = FILE_TYPES[mime].format(full_path, cap) if format in FILE_TYPES: diff --git a/setup.py b/setup.py index efe3376..b1e00ec 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,6 @@ from setuptools import find_packages, setup dependencies = [ - # pinned because https://github.com/python-pillow/Pillow/issues/2609 - 'pillow==4.1', 'python-magic==0.4.15', ]