RU
games/satisfying-bounce / tools / site / images.py · commit 8bdd6e4
#!/usr/bin/env python3"""site/images/og.png (1200 x 630, sharing) and assets/thumbnail.png (1280 x 720, the directory) from the covers of the films(dist/video/satisfying-bounce-<template>-3-en.png, made by tools/make-videos.sh): the title and a row of phone-shaped frames."""import osfrom PIL import Image, ImageDraw, ImageFontROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..'))FONT = os.path.join(ROOT, 'assets', 'fonts', 'Quicksand-Bold.ttf')PICKS = ['zoom', 'tunnel', 'stairs', 'cats', 'dominos']def cover(t):    return Image.open(os.path.join(ROOT, 'dist', 'video', f'satisfying-bounce-{t}-3-en.png')).convert('RGB')def phone(img, h):    w = int(h * 9 / 16)    im = img.resize((w, h), Image.LANCZOS)    mask = Image.new('L', (w, h), 0)    ImageDraw.Draw(mask).rounded_rectangle((0, 0, w - 1, h - 1), radius=h // 14, fill=255)    out = Image.new('RGBA', (w, h))    out.paste(im, (0, 0), mask)    return outdef compose(size, n, ph, title_size, out):    w, h = size    bg = Image.new('RGB', size)    d = ImageDraw.Draw(bg)    for y in range(h):        k = y / h        d.line([(0, y), (w, y)], fill=(int(9 + 14 * k), int(10 + 5 * k), int(26 + 20 * k)))    font = ImageFont.truetype(FONT, title_size)    small = ImageFont.truetype(FONT, int(title_size * 0.42))    d.text((int(w * 0.05), int(h * 0.22)), 'SATISFYING', font=font, fill=(255, 255, 255))    d.text((int(w * 0.05), int(h * 0.22) + int(title_size * 1.05)), 'BOUNCE', font=font, fill=(255, 214, 90))    d.text((int(w * 0.05), int(h * 0.22) + int(title_size * 2.3)), '15 satisfying videos and illusions', font=small, fill=(190, 198, 240))    x = int(w * 0.47)    gap = int(ph * 0.07)    for i, t in enumerate(PICKS[:n]):        p = phone(cover(t), ph)        y = int(h * 0.5 - ph / 2) + (i % 2) * int(ph * 0.06) - int(ph * 0.03)        bg.paste(p, (x + i * (p.width + gap), y), p)    bg.save(out)    print('written', out, bg.size)if __name__ == '__main__':    compose((1200, 630), 3, 330, 66, os.path.join(ROOT, 'site', 'images', 'og.png'))    compose((1280, 720), 3, 370, 72, os.path.join(ROOT, 'assets', 'thumbnail.png'))