Files
linkspin/tools/blog-covers.py
T

47 lines
3.2 KiB
Python

# Blog covers (1200x630) from the LinkSpin kit masters: ring-of-light visual, typeset title, wordmark. Neutral file names.
# Masters + fonts: copy lskit/masters/*.png and lskit/fonts/*.ttf next to this script (they live in the session scratch dir).
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import json, pathlib, sys
HERE = pathlib.Path(__file__).parent
SRC = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else HERE # dir holding masters/ and fonts/
OUT = HERE.parent / 'public' / 'banners'
POSTS = json.loads((HERE / 'blog-seed.json').read_text(encoding='utf-8'))
LAND = Image.open(SRC / 'masters/master-landscape.png').convert('RGB'); SQ = Image.open(SRC / 'masters/master-square.png').convert('RGB')
def FD(px): return ImageFont.truetype(str(SRC / 'fonts/Rajdhani-Bold.ttf'), px)
def FB(px, w='SemiBold'):
f = ImageFont.truetype(str(SRC / 'fonts/Manrope.ttf'), px); f.set_variation_by_name(w); return f
BLUE = (59, 157, 255); ORANGE = (255, 106, 26); FG = (236, 242, 250); MUTED = (176, 192, 216)
def crop_to(im, w, h, focus):
W, H = im.size; r = w / h
cw, ch = (int(H * r), H) if W / H > r else (W, int(W / r))
cx, cy = int(focus[0] * W), int(focus[1] * H); x = min(max(cx - cw // 2, 0), W - cw); y = min(max(cy - ch // 2, 0), H - ch)
return im.crop((x, y, x + cw, y + ch)).resize((w, h), Image.LANCZOS)
def shade(im, box, strength, blur):
x0, y0, x1, y1 = [int(v) for v in box]; ov = Image.new('L', im.size, 0); d = ImageDraw.Draw(ov)
d.rounded_rectangle((x0, y0, x1, y1), radius=40, fill=int(255 * strength)); ov = ov.filter(ImageFilter.GaussianBlur(blur))
im.paste(Image.new('RGB', im.size, (7, 12, 24)), (0, 0), ov); return im
def wrap(d, text, f, maxw):
words = text.split(); lines, cur = [], ''
for w in words:
t = (cur + ' ' + w).strip()
if d.textlength(t, font=f) > maxw and cur: lines.append(cur); cur = w
else: cur = t
if cur: lines.append(cur)
return lines
FOCI = [(0.36, 0.5), (0.5, 0.52), (0.36, 0.5), (0.5, 0.52), (0.36, 0.5)]
for i, p in enumerate(POSTS):
src = LAND if i % 2 == 0 else SQ; W, H = 1200, 630
im = crop_to(src, W, H, FOCI[i]); shade(im, (0, 0, W * 0.6, H), 0.72, 40); d = ImageDraw.Draw(im)
x = 64; y = 60; f = FB(28, 'Bold'); d.text((x, y), 'Link', font=f, fill=FG); d.text((x + d.textlength('Link', font=f), y), 'Spin', font=f, fill=BLUE)
d.text((x + d.textlength('LinkSpin', font=f) + 18, y + 6), 'BLOG', font=FB(18, 'Bold'), fill=MUTED)
title = p['title'].split(':')[0] if ':' in p['title'] else p['title']
fh = FD(74); lines = wrap(d, title.upper(), fh, 640)
while len(lines) > 3 and fh.size > 44: fh = FD(fh.size - 4); lines = wrap(d, title.upper(), fh, 640)
y = 150
for k, line in enumerate(lines): d.text((x, y), line, font=fh, fill=ORANGE if k == len(lines) - 1 else FG); y += int(fh.size * 0.92)
sub = p['excerpt'].split('. ')[0] + '.'
fs = FB(23); y += 16
for line in wrap(d, sub, fs, 620)[:3]: d.text((x, y), line, font=fs, fill=MUTED); y += 34
d.text((x, H - 70), 'linkspin.app/blog', font=FB(20, 'Bold'), fill=BLUE)
out = OUT / p['cover'].split('/')[-1]; im.save(out, 'JPEG', quality=86, optimize=True, progressive=True); print(out.name)