1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
8 from ..base import WLElement
11 class Ilustr(WLElement):
14 EPUB_TAG = HTML_TAG = 'img'
16 def get_html_attr(self, builder):
19 url = urllib.parse.urljoin(
24 imgfile = urllib.request.urlopen(url)
25 img = Image.open(imgfile)
26 th_format, ext, media_type = {
27 'GIF': ('GIF', 'gif', 'image/gif'),
28 'PNG': ('PNG', 'png', 'image/png'),
29 }.get(img.format, ('JPEG', 'jpg', 'image/jpeg'))
32 if img.size[0] < width:
35 th = img.resize((width, round(width * img.size[1] / img.size[0])))
38 th.save(buffer, format=th_format)
40 file_name = 'image%d.%s' % (
41 builder.assign_image_number(),
46 content=buffer.getvalue(),
48 media_type=media_type,
53 'alt': self.attrib.get('alt', ''),
54 'title': self.attrib.get('alt', ''),
57 get_epub_attr = get_html_attr