From 9512004e93f97e7abc0c7078b6c806d86b098e7e Mon Sep 17 00:00:00 2001 From: Dooho Yi Date: Wed, 1 Dec 2021 12:30:32 +0000 Subject: [PATCH] alt support added for thumbnailed images --- distribusi/distribusi/distribusi.py | 54 ++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/distribusi/distribusi/distribusi.py b/distribusi/distribusi/distribusi.py index f6eb696..8fed226 100644 --- a/distribusi/distribusi/distribusi.py +++ b/distribusi/distribusi/distribusi.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import base64 import os @@ -43,9 +43,10 @@ def caption(image): def thumbnail(image, name, args, size=(450,450)): - + try: # size = (450, 450) + alt = '' im = Image.open(image) exif = None try: @@ -65,12 +66,12 @@ def thumbnail(image, name, args, size=(450,450)): im = im.rotate(270, expand=True) elif exif[orientation] == 8: im = im.rotate(90, expand=True) - + if (im.mode == 'RGBA'): bg = Image.new('RGBA', im.size, (255,255,255)) composite = Image.alpha_composite(bg, im) im=composite.convert('RGB') - + output = BytesIO() im.save(output, format='JPEG') im_data = output.getvalue() @@ -79,13 +80,21 @@ def thumbnail(image, name, args, size=(450,450)): cap = caption(image) else: cap = name + # alt + alt_path = image + ".alt" + if os.path.isfile(alt_path): + f = open(alt_path, 'r', encoding='utf-8') + while True: + line = f.readline() + if not line: break + alt = alt + line + ' ' return ( - "
{}
" - ).format(name, data_url, cap) + "
{}
{}
" + ).format(name, data_url, alt, cap) except Exception as e: traceback.print_exc() print('Thumbnailer:', e) - return "
{}
".format(name, name, name) + return "
{}
{}
".format(name, name, alt, name) def div(args, type_, subtype, tag, name, fid): @@ -202,15 +211,6 @@ def render_dir(args, directory): if type_ == 'image': a = FILE_TYPES[type_].format(relative_path, c, c) - if args.thumbnail: - thumbconf_path = "./{}/{}".format(root, "thumbconf.json") - size = (450, 450) - if os.path.isfile(thumbconf_path): - with open(thumbconf_path) as json_file: - json_data = json.load(json_file) - size = tuple(json_data['size']) - print("applying thumbconf.json: size: ", size) - a = thumbnail(full_path, relative_path, args, size) if args.no_filenames: c = "" if args.captions: @@ -227,6 +227,16 @@ def render_dir(args, directory): alt = alt + line + ' ' a = FILE_TYPES[type_].format(relative_path, alt, c) + # if thumbnail, override. + if args.thumbnail: + thumbconf_path = "./{}/{}".format(root, "thumbconf.json") + size = (450, 450) + if os.path.isfile(thumbconf_path): + with open(thumbconf_path) as json_file: + json_data = json.load(json_file) + size = tuple(json_data['size']) + print("applying thumbconf.json: size: ", size) + a = thumbnail(full_path, relative_path, args, size) if subtype in SUB_TYPES: a = SUB_TYPES[subtype] @@ -299,7 +309,7 @@ def distribusify(args, directory, freg): # noqa print('Found', name, 'as', mime) if type_ in FILE_TYPES: - + a = FILE_TYPES[type_].format(name, c, c) # expansion for different kind of text files @@ -339,6 +349,16 @@ def distribusify(args, directory, freg): # noqa alt = alt + line + ' ' a = FILE_TYPES[type_].format(name, alt, c) + # # if thumbnail, override. + # if args.thumbnail: + # thumbconf_path = "./{}/{}".format(root, "thumbconf.json") + # size = (450, 450) + # if os.path.isfile(thumbconf_path): + # with open(thumbconf_path) as json_file: + # json_data = json.load(json_file) + # size = tuple(json_data['size']) + # print("applying thumbconf.json: size: ", size) + # a = thumbnail(full_path, relative_path, args, size) if subtype in SUB_TYPES: a = SUB_TYPES[subtype]