#!/usr/bin/env python3 """Compone le cover dello store sovrapponendo il testo alle immagini generate. Il testo si scrive con PIL, non si delega al generatore: i modelli sbagliano le lettere. Le immagini raw restano come sfondo, il testo va nella metà sinistra che le tre varianti lasciano libera. """ from pathlib import Path from PIL import Image, ImageDraw, ImageFont HERE = Path(__file__).resolve().parent TITLE = "Thinq-like" CLAIM = "Your LG washing machine, from your phone" NOTE = "Sailfish OS \u00b7 straight to LG, no server in between" FONT_DIR = Path("/usr/share/fonts/truetype/dejavu") BOLD = FONT_DIR / "DejaVuSans-Bold.ttf" REG = FONT_DIR / "DejaVuSans.ttf" # variante -> colore del testo (immagine chiara = testo scuro, immagine blu = chiaro) VARIANTS = { "a": (16, 44, 74), # fotografia chiara "b": (255, 255, 255), # illustrazione blu "c": (26, 38, 32), # still life caldo } ACCENT = (26, 127, 158) def fit_font(draw, text, path, target_width, start=120, minimum=24): """Rimpicciolisce il font finche' il testo sta nella larghezza data.""" size = start while size > minimum: font = ImageFont.truetype(str(path), size) if draw.textlength(text, font=font) <= target_width: return font size -= 2 return ImageFont.truetype(str(path), minimum) def compose(tag, color): src = HERE / f"raw-{tag}.jpg" if not src.exists(): src = HERE / f"raw-{tag}.png" img = Image.open(src).convert("RGB") w, h = img.size draw = ImageDraw.Draw(img) margin = int(w * 0.06) limit = int(w * 0.48) title_font = fit_font(draw, TITLE, BOLD, limit, start=104) claim_font = fit_font(draw, CLAIM, REG, limit, start=40) note_font = fit_font(draw, NOTE, REG, limit, start=28) block = (title_font.size + 18 + claim_font.size + 22 + note_font.size) y = (h - block) // 2 # filetto d'accento sopra il titolo: da' un appiglio grafico al blocco draw.rectangle([margin, y - 28, margin + 64, y - 22], fill=ACCENT) draw.text((margin, y), TITLE, font=title_font, fill=color) y += title_font.size + 18 draw.text((margin, y), CLAIM, font=claim_font, fill=color) y += claim_font.size + 22 draw.text((margin, y), NOTE, font=note_font, fill=color) out = HERE / f"cover-{tag}.jpg" img.save(out, quality=92) print(f"{out.name} {w}x{h} {out.stat().st_size // 1024} KB") return out if __name__ == "__main__": for tag, color in VARIANTS.items(): compose(tag, color)