Browse Source

speed up by not calling exiftools always, images now always have figcaptions, catch errors properly

master
rra 5 years ago
parent
commit
b613eef36c
  1. 38
      distribusi/distribusi.py
  2. 2
      setup.py

38
distribusi/distribusi.py

@ -7,27 +7,18 @@ import magic
from PIL import Image from PIL import Image
from distribusi.page_template import html_footer, html_head from distribusi.page_template import html_footer, html_head
from distribusi.mappings import CODE_TYPES, FILE_TYPES
CODE_TYPES = ['x-c', 'html']
FILE_TYPES = {
'image': '<figure><img class="image" src="{}">{}</figure>',
'pdf': (
'<object data="{}" class="pdf" type="application/pdf">'
'<embed src="{}" type="application/pdf" /></object>'
),
'text': '<a href="{}" class="text">{}</a>',
'video': ('<video class="video" controls>' '<source src="{}"></source></video>'),
'audio': ('<audio controls class="audio">' '<source src="{}"></source></audio>'),
}
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
def caption(image): def caption(image):
try:
process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE) process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE)
out, err = process.communicate() out, err = process.communicate()
except Exception as e:
print(e)
print('Do you have exiftool installed?')
try: try:
caption = out.decode("utf-8").split(": ", 1)[1] caption = out.decode("utf-8").split(": ", 1)[1]
except: except:
@ -36,6 +27,7 @@ def caption(image):
def thumbnail(image, name, args): def thumbnail(image, name, args):
try:
size = (450, 450) size = (450, 450)
im = Image.open(image) im = Image.open(image)
im.thumbnail(size) im.thumbnail(size)
@ -43,14 +35,17 @@ def thumbnail(image, name, args):
im.save(output, format='JPEG') im.save(output, format='JPEG')
im_data = output.getvalue() im_data = output.getvalue()
data_url = base64.b64encode(im_data).decode() data_url = base64.b64encode(im_data).decode()
if args.captions:
cap = caption(image) cap = caption(image)
if cap and args.captions:
cap = "<figcaption>{}</figcaption>".format(cap)
else: else:
cap = '' cap = name
return ( return (
"<figure><a href='{}'><img class='thumbnail' src='data:image/jpg;base64,{}'></a>{}</figure>" "<figure><a href='{}'><img class='thumbnail' src='data:image/jpg;base64,{}'></a><figcaption>{}</figcaption></figure>"
).format(name, data_url, cap) ).format(name, data_url, cap)
except Exception as e:
print(e)
return "<figure><a href='{}'><img class='thumbnail' src='{}'></a><figcaption>{}</figcaption></figure>".format(name, name,name)
def div(args, mime, tag, *values): def div(args, mime, tag, *values):
@ -60,7 +55,7 @@ def div(args, mime, tag, *values):
else: else:
filename = '' filename = ''
if 'image' in mime: if 'image' in mime:
html = '<div id="{}">{}' + filename + '</div>' html = '<div id="{}">{}</div>'
elif 'pdf' in mime: elif 'pdf' in mime:
html = '<div id="{}">{}' + filename + '</div>' html = '<div id="{}">{}' + filename + '</div>'
else: else:
@ -103,11 +98,10 @@ def distribusify(args, directory): # noqa
if mime == 'image' and args.thumbnail: if mime == 'image' and args.thumbnail:
a = thumbnail(full_path, name, args) a = thumbnail(full_path, name, args)
else: else:
if args.captions:
cap = caption(full_path) cap = caption(full_path)
if cap and args.captions:
cap = "<figcaption>{}</figcaption>".format(cap)
else: else:
cap = '' cap = name
a = FILE_TYPES[mime].format(full_path, cap) a = FILE_TYPES[mime].format(full_path, cap)
if format in FILE_TYPES: if format in FILE_TYPES:

2
setup.py

@ -1,8 +1,6 @@
from setuptools import find_packages, setup from setuptools import find_packages, setup
dependencies = [ dependencies = [
# pinned because https://github.com/python-pillow/Pillow/issues/2609
'pillow==4.1',
'python-magic==0.4.15', 'python-magic==0.4.15',
] ]

Loading…
Cancel
Save