X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/0604bdd5f693da9f1c78f9d9fa2276f0c7b6c17b..261a3308d235fc252f30ae03603d0e964bb60223:/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 bd51453..ab7c2b7 100644 --- a/src/librarian/elements/figures/ilustr.py +++ b/src/librarian/elements/figures/ilustr.py @@ -1,10 +1,51 @@ +import six.moves +from PIL import Image from ..base import WLElement class Ilustr(WLElement): - HTML_TAG = 'img' + EPUB_TAG = HTML_TAG = 'img' def get_html_attr(self, builder): + ## TODO: thumbnail. + + url = six.moves.urllib.parse.urljoin( + builder.base_url, + self.get('src') + ) + + imgfile = six.moves.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]))) + + imgfile.close() + buffer = six.BytesIO() + th.save(buffer, format=th_format) + ## TODO: Counter + 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.image_location + self.attrib['src'] + 'src': file_name, + 'alt': self.attrib['alt'], + 'title': self.attrib['alt'], } + + get_epub_attr = get_html_attr