1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
6 from librarian import get_resource
7 from .base import Widget
10 class ImageWidget(Widget):
11 def build(self, w, h):
12 img = PIL.Image.open(self.image_path)
13 img = img.resize((round(img.size[0] / img.size[1] * h), h))
17 class WLLogo(ImageWidget):
20 if self.cover.color_scheme['text'] == '#fff':
21 return get_resource('res/cover/logo_WL_invert.png')
23 return get_resource('res/cover/logo_WL.png')
26 class Label(ImageWidget):
29 if self.cover.is_very_bright:
30 return get_resource('res/cover/label_WLpolecaja.szary.png')
32 return get_resource('res/cover/label_WLpolecaja.png')
35 class LogoSticker(ImageWidget):
36 def __init__(self, cover, image_path):
37 self.image_path = image_path
39 def apply(self, img, x, y, w, h, padding_x, padding_y, radius):
40 my_img = self.build(w, h, padding_x, padding_y, radius)
41 if my_img is not None:
44 (round(x), round(y - my_img.size[1])),
45 my_img if self.transparency else None
49 def build(self, w, h, padding_x, padding_y, radius):
50 img = PIL.Image.open(self.image_path)
51 uw, uh = w - 2 * padding_x, h - 2 * padding_y
52 if img.size[1] / img.size[0] > uh / uw:
53 img = img.resize((round(img.size[0] / img.size[1] * uh), uh))
55 img = img.resize((uw, round(img.size[1] / img.size[0] * uw)))
56 sz = w, img.size[1] + 2 * padding_y
57 sticker = PIL.Image.new('RGBA', sz)
58 draw = PIL.ImageDraw.Draw(sticker)
59 draw.rectangle((0, radius, sz[0], sz[1] - radius), (0, 0, 0))
60 draw.rectangle((radius, 0, sz[0] - radius, sz[1]), (0, 0, 0))
61 draw.ellipse((0, 0, 2 * radius, 2 * radius), (0, 0, 0))
62 draw.ellipse((0, sz[1] - 2 * radius, 2 * radius, sz[1]), (0, 0, 0))
63 draw.ellipse((sz[0] - 2 * radius, 0, sz[0], 2 * radius), (0, 0, 0))
64 draw.ellipse((sz[0] - 2 * radius, sz[1] - 2 * radius, sz[0], sz[1]), (0, 0, 0))
68 round(padding_x + (uw - img.size[0]) / 2), padding_y),