From d3d58ca2b55f820b1d5ce30260793e0848bb1247 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 30 Jan 2024 17:28:04 +0100 Subject: [PATCH] Support cover logo. --- src/librarian/cover.py | 1 + src/librarian/covers/marquise.py | 15 +++++++++-- src/librarian/covers/widgets/image.py | 39 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/librarian/cover.py b/src/librarian/cover.py index a3bd5cd..3581f59 100644 --- a/src/librarian/cover.py +++ b/src/librarian/cover.py @@ -163,6 +163,7 @@ class Cover: if format is not None: self.format = format self.set_size(width, height) + self.cover_logo = cover_logo def set_size(self, width, height): if width and height: diff --git a/src/librarian/covers/marquise.py b/src/librarian/covers/marquise.py index 6f33b32..0d64e9c 100644 --- a/src/librarian/covers/marquise.py +++ b/src/librarian/covers/marquise.py @@ -7,7 +7,7 @@ from .utils.color import algo_contrast_or_hue, luminance, is_very_bright from .utils.textbox import DoesNotFit from .widgets.author import AuthorBox from .widgets.background import Background -from .widgets.image import WLLogo, Label +from .widgets.image import WLLogo, Label, LogoSticker from .widgets.marquise import Marquise from .widgets.title import TitleBox @@ -21,6 +21,10 @@ class MarquiseCover(Cover): margin = 100 logo_h = 177 author_width = 1300 + sticker_size = 400 + sticker_padding_x = 30 + sticker_padding_y = 50 + sticker_radius = 10 title_box_top = 262 @@ -160,7 +164,14 @@ class MarquiseCover(Cover): ) WLLogo(self).apply(img, self.m.margin, self.m.margin, None, self.m.logo_h) - + + if self.cover_logo: + LogoSticker(self, self.cover_logo).apply( + img, + self.m.width - self.m.margin - self.m.sticker_size, + self.m.height - self.m.margin, + self.m.sticker_size, self.m.sticker_size, self.m.sticker_padding_x, self.m.sticker_padding_y, self.m.sticker_radius + ) for logo in self.additional_logos: LogoSticker(self, logo).apply(img, 0, 0) diff --git a/src/librarian/covers/widgets/image.py b/src/librarian/covers/widgets/image.py index 798dfaf..af1e687 100644 --- a/src/librarian/covers/widgets/image.py +++ b/src/librarian/covers/widgets/image.py @@ -2,6 +2,7 @@ # Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # import PIL.Image +import PIL.ImageDraw from librarian import get_resource from .base import Widget @@ -30,3 +31,41 @@ class Label(ImageWidget): else: return get_resource('res/cover/label_WLpolecaja.png') + +class LogoSticker(ImageWidget): + def __init__(self, cover, image_path): + self.image_path = image_path + + def apply(self, img, x, y, w, h, padding_x, padding_y, radius): + my_img = self.build(w, h, padding_x, padding_y, radius) + if my_img is not None: + img.paste( + my_img, + (round(x), round(y - my_img.size[1])), + my_img if self.transparency else None + ) + + + def build(self, w, h, padding_x, padding_y, radius): + img = PIL.Image.open(self.image_path) + uw, uh = w - 2 * padding_x, h - 2 * padding_y + if img.size[1] / img.size[0] > uh / uw: + img = img.resize((round(img.size[0] / img.size[1] * uh), uh)) + else: + img = img.resize((uw, round(img.size[1] / img.size[0] * uw))) + sz = w, img.size[1] + 2 * padding_y + sticker = PIL.Image.new('RGBA', sz) + draw = PIL.ImageDraw.Draw(sticker) + draw.rectangle((0, radius, sz[0], sz[1] - radius), (0, 0, 0)) + draw.rectangle((radius, 0, sz[0] - radius, sz[1]), (0, 0, 0)) + draw.ellipse((0, 0, 2 * radius, 2 * radius), (0, 0, 0)) + draw.ellipse((0, sz[1] - 2 * radius, 2 * radius, sz[1]), (0, 0, 0)) + draw.ellipse((sz[0] - 2 * radius, 0, sz[0], 2 * radius), (0, 0, 0)) + draw.ellipse((sz[0] - 2 * radius, sz[1] - 2 * radius, sz[0], sz[1]), (0, 0, 0)) + sticker.paste( + img, + ( + round(padding_x + (uw - img.size[0]) / 2), padding_y), + img + ) + return sticker -- 2.20.1