X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/83cae63af4330912cdb2546c195af2919afd30ac..9c9a55d7dda5595a236f11977d7f21f804d37872:/src/librarian/elements/figures/ilustr.py?ds=inline diff --git a/src/librarian/elements/figures/ilustr.py b/src/librarian/elements/figures/ilustr.py index 3c3026c..33099f6 100644 --- a/src/librarian/elements/figures/ilustr.py +++ b/src/librarian/elements/figures/ilustr.py @@ -1,12 +1,57 @@ +# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. +# +import io +import urllib.parse +import urllib.request +from PIL import Image from ..base import WLElement class Ilustr(WLElement): - HTML_TAG = 'img' + SHOULD_HAVE_ID = True + + EPUB_TAG = HTML_TAG = 'img' def get_html_attr(self, builder): + ## TODO: thumbnail. + + url = urllib.parse.urljoin( + builder.base_url, + self.get('src') + ) + + imgfile = urllib.request.urlopen(url) + img = Image.open(imgfile) + th_format, ext, media_type = { + 'GIF': ('GIF', 'gif', 'image/gif'), + 'PNG': ('PNG', 'png', 'image/png'), + }.get(img.format, ('JPEG', 'jpg', 'image/jpeg')) + + width = 1200 + if img.size[0] < width: + th = img + else: + th = img.resize((width, round(width * img.size[1] / img.size[0]))) + + buffer = io.BytesIO() + th.save(buffer, format=th_format) + imgfile.close() + file_name = 'image%d.%s' % ( + builder.assign_image_number(), + ext + ) + + builder.add_file( + content=buffer.getvalue(), + file_name=file_name, + media_type=media_type, + ) + return { - 'src': builder.base_url + self.attrib['src'], - 'alt': self.attrib['alt'], - 'title': self.attrib['alt'], + 'src': file_name, + 'alt': self.attrib.get('alt', ''), + 'title': self.attrib.get('alt', ''), } + + get_epub_attr = get_html_attr