Files
LihonT/scripts/make_thief_video.py
cheb0chik 2907c99c44 LihonT 3 Luna alpha 1: the lock, the journal of entries and the front-camera picture
Lock for the whole app, every chat or chosen chats, opened by the phone's own fingerprint, face or lock-screen code. A journal of entries
(time, result, wrong fingers, chats opened) with optional front-camera pictures kept in a private folder, and a clear-all button.
Also: the mascot and the promo scripts for this release.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 15:11:30 +03:00

701 lines
35 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The promo skit for the lock of LihonT 3 · Luna alpha 1: at night a thief takes a phone and cannot open it (the front camera clicks), in the
morning the owner opens the phone with his finger and finds the thief's picture in the journal of entries, then the calico cat (the mascot,
scripts/make_release_video.py) tells about the update and where to get it. Everyone in it is drawn here: no real person, no real picture.
py -X utf8 -B scripts/make_thief_video.py # work/thief-video/thief.mp4 (16:9)
py -X utf8 -B scripts/make_thief_video.py --vertical # 9:16
py -X utf8 -B scripts/make_thief_video.py --still 4.0 # one frame
"""
from __future__ import annotations
import argparse
import math
import shutil
import subprocess
import sys
from functools import lru_cache
from multiprocessing import Pool
from pathlib import Path
import numpy as np
from PIL import Image, ImageChops, ImageDraw, ImageFilter
sys.path.insert(0, str(Path(__file__).resolve().parent))
import make_release_video as R # noqa: E402
import make_store_music as M # noqa: E402
import make_store_video as V # noqa: E402
from make_store_video import INK, clamp, font, in_out, lerp, out_back, out_cubic, put_c, seg # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "work" / "thief-video"
FPS = 30
SS = 2
W = H = 0
VERT = False
# the timeline (seconds)
T_DAY = 7.6 # the morning begins
T_CAT = 15.6 # the cat scene takes over (its own clock starts a quarter of a second earlier: no fade-in)
CAT_OFFSET = T_CAT - 0.25
CAT_LINES = [
"Мяу! Вышла *alpha 1*",
"Блокировка, *фото вора* и журнал входов",
"Всё остаётся только на твоём телефоне",
"Скачать: *lihont.space* и Telegram *@lihont*",
]
SKIN = (244, 200, 160)
SKIN_DARK = (226, 170, 130)
DARK = (46, 52, 78)
DARKER = (30, 34, 54)
BEANIE = (38, 38, 46)
RED = (214, 60, 70)
def setup(vertical: bool) -> None:
global W, H, VERT
R.setup(vertical)
W, H, VERT = V.W, V.H, bool(vertical)
R.TITLE = "alpha 1"
HALF = 374 # half the width of the phone picture, at scale 1
def layout() -> dict:
"""Where things stand: the phone, the table, the people."""
if VERT:
return dict(phone=(W // 2, 1160), table=1470, feet=1580, thief_x=190, guy_x=W - 190, person=0.8, base=0.5, zoom=1.3)
return dict(phone=(W // 2, 620), table=930, feet=990, thief_x=W // 2 - 430, guy_x=W // 2 + 430, person=0.95, base=0.5, zoom=0.76)
# ---------------------------------------------------------------- small drawing helpers (all at SS times the size)
def draw_geom(d, kind, g, fill, grow=0, sx=0.0, sy=0.0):
k = SS
if kind == "ell":
cx, cy, rx, ry = g
d.ellipse([(cx + sx - rx - grow) * k, (cy + sy - ry - grow) * k, (cx + sx + rx + grow) * k, (cy + sy + ry + grow) * k], fill=fill)
elif kind == "rr":
x0, y0, x1, y1, r = g
d.rounded_rectangle([(x0 + sx - grow) * k, (y0 + sy - grow) * k, (x1 + sx + grow) * k, (y1 + sy + grow) * k], radius=(r + grow) * k, fill=fill)
elif kind == "poly":
if len(g) == 1 and isinstance(g[0], list):
g = g[0]
pts = [((x + sx) * k, (y + sy) * k) for x, y in g]
d.polygon(pts, fill=fill)
if grow:
d.line(pts + [pts[0]], fill=fill, width=int(2 * grow * k), joint="curve")
elif kind == "line":
p0, p1, w = g
ww = (w + 2 * grow) * k
d.line([((p0[0] + sx) * k, (p0[1] + sy) * k), ((p1[0] + sx) * k, (p1[1] + sy) * k)], fill=fill, width=int(ww))
for pt in (p0, p1):
r = ww / 2
d.ellipse([(pt[0] + sx) * k - r, (pt[1] + sy) * k - r, (pt[0] + sx) * k + r, (pt[1] + sy) * k + r], fill=fill)
def mask_of(img, kind, g, grow=0, sx=0.0, sy=0.0):
m = Image.new("L", img.size, 0)
draw_geom(ImageDraw.Draw(m), kind, g, 255, grow, sx, sy)
return m
def S(img, kind, g, fill, ow=7, shade=True, light=(-10, -13), dark=0.72, hl=None):
"""One shape of a character: an ink outline, a darker colour, the real colour on the lit side (cel shading) and, if asked, a shine."""
d = ImageDraw.Draw(img)
if ow:
draw_geom(d, kind, g, INK + (255,), grow=ow)
base = mask_of(img, kind, g)
shadow = tuple(int(c * dark) for c in fill)
img.paste(shadow + (255,), mask=base) if shade else img.paste(tuple(fill) + (255,), mask=base)
if shade:
lit = ImageChops.darker(base, mask_of(img, kind, g, 0, light[0], light[1]))
img.paste(tuple(fill) + (255,), mask=lit)
if hl:
cx, cy, rx, ry = hl
shine = Image.new("L", img.size, 0)
ImageDraw.Draw(shine).ellipse([(cx - rx) * SS, (cy - ry) * SS, (cx + rx) * SS, (cy + ry) * SS], fill=95)
img.paste((255, 255, 255, 255), mask=ImageChops.darker(shine, base))
def eye(img, cx, cy, mood, look, blink, iris=(96, 62, 40), narrow=False):
d = ImageDraw.Draw(img)
if blink:
d.arc([(cx - 22) * SS, (cy - 14) * SS, (cx + 22) * SS, (cy + 14) * SS], 200, 340, fill=INK + (255,), width=6 * SS)
return
rx, ry = (21, 12) if narrow else ((27, 29) if mood == "shock" else (22, 22))
S(img, "ell", (cx, cy, rx, ry), (255, 255, 255), ow=5, shade=False)
ix, iy = cx + look[0] * 6, cy + look[1] * 4
ir = 9 if mood == "shock" else (8 if narrow else 12)
S(img, "ell", (ix, iy, ir, ir), iris, ow=0, shade=False)
S(img, "ell", (ix, iy, ir * 0.55, ir * 0.55), (20, 14, 14), ow=0, shade=False)
d.ellipse([(ix - ir * 0.5 - 3) * SS, (iy - ir * 0.6 - 3) * SS, (ix - ir * 0.5 + 3) * SS, (iy - ir * 0.6 + 3) * SS], fill=(255, 255, 255, 255))
def person(kind: str, step: float = 0.0, arm=None, mood: str = "calm", look=(1, 0), size: float = 1.0, blink: bool = False) -> Image.Image:
"""A character 400x700 big (before `size`), feet at the bottom middle. `arm` is where the near hand reaches, in the same units."""
img = Image.new("RGBA", (400 * SS, 700 * SS), (0, 0, 0, 0))
d = ImageDraw.Draw(img)
thief = kind == "thief"
skin = SKIN if thief else (238, 190, 150)
skin_shadow = SKIN_DARK
# legs with a swing
for side, phase in ((-1, 0.0), (1, math.pi)):
swing = math.sin(step + phase)
lift = max(0.0, math.cos(step + phase)) * 16 if abs(step) > 0 else 0.0
x = 200 + side * 42 + swing * 20
S(img, "line", ((200 + side * 42, 520), (x, 662 - lift), 56), (36, 36, 46) if thief else (70, 98, 168), ow=7)
S(img, "ell", (x + 12, 674 - lift, 42, 18), (30, 30, 36) if thief else (170, 112, 66), ow=6)
if thief: # the sack over the shoulder
S(img, "ell", (92, 410, 64, 80), (172, 126, 80), ow=7, hl=(70, 372, 20, 26))
S(img, "poly", ([(76, 336), (108, 336), (122, 308), (98, 322), (70, 304)], ), (172, 126, 80), ow=6) if False else None
d.text((92 * SS, 410 * SS), "$", font=font(76 * SS, "black"), fill=(255, 238, 170, 255), anchor="mm")
# body
if thief:
S(img, "rr", (100, 292, 300, 562, 60), (250, 250, 252), ow=7)
stripes = Image.new("L", img.size, 0)
sd = ImageDraw.Draw(stripes)
for k in range(8):
y0 = 300 + k * 34
sd.rectangle([90 * SS, y0 * SS, 310 * SS, (y0 + 16) * SS], fill=255)
img.paste(INK + (255,), mask=ImageChops.darker(stripes, mask_of(img, "rr", (100, 292, 300, 562, 60))))
S(img, "rr", (100, 292, 300, 562, 60), (0, 0, 0), ow=0, shade=False) if False else None
else:
S(img, "rr", (94, 296, 306, 566, 80), (248, 248, 250), ow=7)
S(img, "ell", (200, 478, 112, 96), (248, 248, 250), ow=0, hl=(160, 430, 24, 34))
# the V of the vest and some chest hair
d.polygon([(168 * SS, 296 * SS), (232 * SS, 296 * SS), (200 * SS, 368 * SS)], fill=skin + (255,))
for k in range(6):
d.line([((186 + k * 5) * SS, (306 + (k % 3) * 8) * SS), ((190 + k * 5) * SS, (318 + (k % 3) * 8) * SS)], fill=(88, 60, 44, 255), width=3 * SS)
S(img, "rr", (92, 518, 308, 586, 30), (70, 98, 168), ow=7) # the trousers
d.rectangle([92 * SS, 540 * SS, 308 * SS, 552 * SS], fill=(40, 40, 54, 255))
S(img, "rr", (180, 536, 220, 556, 5), (232, 190, 80), ow=3, shade=False) # the buckle
# arms
sleeve = (62, 62, 74) if thief else skin
glove = (30, 30, 36) if thief else skin
S(img, "line", ((112, 340), (82, 478), 46), sleeve, ow=7)
S(img, "ell", (82, 494, 25, 25), glove, ow=6)
if arm is None:
S(img, "line", ((288, 340), (318, 478), 46), sleeve, ow=7)
S(img, "ell", (318, 494, 25, 25), glove, ow=6)
else:
S(img, "line", ((288, 340), arm, 46), sleeve, ow=7)
S(img, "ell", (arm[0], arm[1], 26, 26), glove, ow=6)
if not thief:
S(img, "ell", (arm[0] - 16, arm[1] - 14, 10, 12), glove, ow=4, shade=False) # a thumb
# the head
if thief:
S(img, "ell", (200, 222, 118, 108), (38, 38, 46), ow=7) # the hood behind
for sign in (-1, 1):
S(img, "ell", (200 + sign * 100, 224, 17, 25), skin, ow=6) # the ears
S(img, "ell", (200, 214, 98, 96), skin, ow=7, hl=(165, 170, 22, 30))
S(img, "poly", ([(102, 190), (110, 150), (200, 112), (290, 150), (298, 190), (270, 168), (200, 156), (130, 168)], ), BEANIE, ow=6)
S(img, "rr", (102, 166, 298, 204, 18), (84, 86, 98), ow=6) # the folded edge
S(img, "rr", (110, 206, 290, 254, 22), (24, 24, 30), ow=6, shade=False) # the mask
eye(img, 158, 230, mood, look, blink, iris=(110, 86, 60), narrow=True)
eye(img, 242, 230, mood, look, blink, iris=(110, 86, 60), narrow=True)
if mood == "angry":
d.line([(124 * SS, 198 * SS), (178 * SS, 214 * SS)], fill=INK + (255,), width=9 * SS)
d.line([(276 * SS, 198 * SS), (222 * SS, 214 * SS)], fill=INK + (255,), width=9 * SS)
S(img, "ell", (200, 262, 12, 10), skin_shadow, ow=4, shade=False) # the nose
if mood == "angry":
d.arc([(168) * SS, (288) * SS, (232) * SS, (322) * SS], 200, 340, fill=INK + (255,), width=7 * SS)
else:
d.line([(170 * SS, 296 * SS), (230 * SS, 292 * SS)], fill=INK + (255,), width=7 * SS)
for k in range(8):
d.ellipse([(144 + k * 14) * SS, (276 + (k % 2) * 9) * SS, (147 + k * 14) * SS, (279 + (k % 2) * 9) * SS], fill=(96, 74, 64, 255))
else:
S(img, "ell", (200, 228, 108, 100), (138, 120, 104), ow=7) # the hair that is left: round the back and the sides, none on top
for sign in (-1, 1):
S(img, "ell", (200 + sign * 99, 228, 17, 25), skin, ow=6) # the ears
S(img, "ell", (200, 206, 98, 96), skin, ow=7, hl=(160, 140, 30, 22))
eye(img, 158, 210, mood, look, blink)
eye(img, 242, 210, mood, look, blink)
for sign in (-1, 1):
up = -18 if mood == "shock" else 0
S(img, "line", ((200 + sign * 74, 176 + up), (200 + sign * 26, 168 + up + sign * 5), 12), (78, 56, 44), ow=0, shade=False)
S(img, "ell", (200, 246, 19, 21), (232, 156, 124), ow=5, hl=(194, 238, 6, 5)) # a big, red nose
S(img, "ell", (166, 278, 44, 16), (92, 58, 40), ow=0, shade=False) # the moustache with curled ends
S(img, "ell", (234, 278, 44, 16), (92, 58, 40), ow=0, shade=False)
S(img, "ell", (126, 270, 12, 10), (92, 58, 40), ow=0, shade=False)
S(img, "ell", (274, 270, 12, 10), (92, 58, 40), ow=0, shade=False)
for k in range(9): # stubble
d.ellipse([(140 + k * 15) * SS, (298 + (k % 2) * 10) * SS, (143 + k * 15) * SS, (301 + (k % 2) * 10) * SS], fill=(112, 88, 72, 255))
if mood == "shock":
S(img, "ell", (200, 312, 17, 22), (110, 40, 52), ow=5, shade=False)
else:
d.arc([160 * SS, 290 * SS, 240 * SS, 330 * SS], 20, 160, fill=INK + (255,), width=6 * SS)
return img.resize((int(400 * size), int(700 * size)), Image.LANCZOS)
@lru_cache(maxsize=8)
def face_photo(kind: str, side: int) -> Image.Image:
"""The picture the front camera took: the head and shoulders, on a dim background, a little grainy."""
base = person(kind, 0.0, None, "angry" if kind == "thief" else "calm", size=1.0, blink=False)
head = base.crop((60, 70, 340, 360)).resize((side, int(side * 290 / 280)), Image.LANCZOS)
bg = Image.new("RGBA", (side, side), (38, 44, 64, 255))
glow = Image.new("RGBA", (side, side), (0, 0, 0, 0))
ImageDraw.Draw(glow).ellipse([side * 0.1, side * 0.05, side * 0.9, side * 0.95], fill=(120, 150, 210, 70))
bg.alpha_composite(glow.filter(ImageFilter.GaussianBlur(side * 0.08)))
bg.alpha_composite(head, (0, side // 12))
noise = np.random.default_rng(3).integers(-14, 14, (side, side, 1)).astype(np.int16)
arr = np.clip(np.asarray(bg.convert("RGB")).astype(np.int16) + noise, 0, 255).astype(np.uint8)
return Image.fromarray(arr).convert("RGBA")
# ---------------------------------------------------------------- the phone
SCREEN_W, SCREEN_H = 600, 1140
def fingerprint(d, cx, cy, r, color, width=7):
for k, f in enumerate((0.42, 0.62, 0.82, 1.0)):
rr_ = r * f
d.arc([cx - rr_, cy - rr_ * 1.15, cx + rr_, cy + rr_ * 1.15], 205 + k * 4, 335 - k * 4, fill=color, width=width)
d.arc([cx - rr_, cy - rr_ * 1.15, cx + rr_, cy + rr_ * 1.15], 20, 160 - k * 6, fill=color, width=width)
d.line([(cx, cy - r * 0.12), (cx, cy + r * 0.5)], fill=color, width=width)
def screen_lock(phase: float, state: str) -> Image.Image:
"""The cover of the lock with the phone's own unlock sheet: waiting, wrong finger (red), right finger (green)."""
img = Image.new("RGBA", (SCREEN_W, SCREEN_H), (13, 13, 15, 255))
d = ImageDraw.Draw(img)
# the padlock
gold, gold_dark = (238, 190, 107), (214, 150, 52)
cx, cy = SCREEN_W // 2, 300
d.arc([cx - 70, cy - 150, cx + 70, cy + 10], 180, 360, fill=gold_dark, width=26)
d.line([(cx - 70, cy - 70), (cx - 70, cy - 20)], fill=gold_dark, width=26)
d.line([(cx + 70, cy - 70), (cx + 70, cy - 20)], fill=gold_dark, width=26)
d.rounded_rectangle([cx - 105, cy - 30, cx + 105, cy + 120], radius=34, fill=gold)
d.ellipse([cx - 18, cy + 18, cx + 18, cy + 54], fill=(92, 28, 40))
d.rectangle([cx - 8, cy + 40, cx + 8, cy + 90], fill=(92, 28, 40))
d.text((cx, 520), "LihonT: TikTok закрыт", font=font(40, "bold"), fill=(245, 245, 247), anchor="mm")
d.text((cx, 580), "Откройте отпечатком", font=font(28, "reg"), fill=(160, 160, 168), anchor="mm")
# the unlock sheet of the phone at the bottom
d.rounded_rectangle([20, 700, SCREEN_W - 20, SCREEN_H - 20], radius=44, fill=(36, 36, 42))
d.text((cx, 770), "Подтвердите, что это вы", font=font(30, "semi"), fill=(240, 240, 244), anchor="mm")
color = {"wait": (150, 190, 255), "wrong": (255, 90, 100), "right": (90, 220, 140)}[state]
pulse = 1.0 + 0.05 * math.sin(phase * 6) if state == "wait" else 1.0
fingerprint(d, cx, 930, 92 * pulse, color, 8)
msg = {"wait": "Приложите палец", "wrong": "Не распознано. Ещё раз", "right": "Готово"}[state]
d.text((cx, 1065), msg, font=font(26, "reg"), fill=color, anchor="mm")
return img
def screen_journal(highlight: float) -> Image.Image:
"""Settings → Privacy → Journal of entries: the newest entry (the night one) with the thief's picture, an older one with the owner's."""
img = Image.new("RGBA", (SCREEN_W, SCREEN_H), (255, 255, 255, 255))
d = ImageDraw.Draw(img)
d.rectangle([0, 0, SCREEN_W, 120], fill=(244, 244, 246))
d.text((40, 70), "←", font=font(44, "bold"), fill=(13, 13, 15), anchor="lm")
d.text((110, 70), "Журнал входов", font=font(38, "bold"), fill=(13, 13, 15), anchor="lm")
rows = [("12 ноя, 03:12 · Не открыто", "Весь TikTok", "Неверных попыток: 2", "thief"), ("11 ноя, 22:40 · Открыто", "Весь TikTok", "Открывал чаты: Аня", "guy")]
y = 150
for i, (title, lock, extra, kind) in enumerate(rows):
h = 250
if i == 0 and highlight > 0:
d.rounded_rectangle([14, y - 8, SCREEN_W - 14, y + h - 12], radius=30, fill=(255, 238, 240), outline=(255, 99, 150), width=int(2 + 5 * highlight))
photo = face_photo(kind, 190)
mask = Image.new("L", photo.size, 0)
ImageDraw.Draw(mask).rounded_rectangle([0, 0, photo.width, photo.height], radius=24, fill=255)
img.paste(photo, (36, y + 14), mask)
d.text((250, y + 48), title.split(" · ")[0], font=font(30, "bold"), fill=(13, 13, 15), anchor="lm")
color = (214, 60, 70) if kind == "thief" else (40, 140, 80)
d.text((250, y + 94), title.split(" · ")[1], font=font(30, "bold"), fill=color, anchor="lm")
d.text((250, y + 138), lock, font=font(26, "reg"), fill=(107, 107, 115), anchor="lm")
d.text((250, y + 176), extra, font=font(26, "reg"), fill=(107, 107, 115), anchor="lm")
y += h + 12
d.rounded_rectangle([40, SCREEN_H - 150, SCREEN_W - 40, SCREEN_H - 70], radius=40, fill=(244, 244, 246))
d.text((SCREEN_W // 2, SCREEN_H - 110), "Очистить всё", font=font(30, "semi"), fill=(13, 13, 15), anchor="mm")
return img
@lru_cache(maxsize=64)
def phone(screen_key: tuple, camera: bool) -> Image.Image:
kind = screen_key[0]
screen = screen_lock(screen_key[1] / 10.0, screen_key[2]) if kind == "lock" else screen_journal(screen_key[1] / 10.0)
pad = 34
w, h = SCREEN_W + 2 * pad, SCREEN_H + 2 * pad + 20
out = Image.new("RGBA", (w + 80, h + 80), (0, 0, 0, 0))
d = ImageDraw.Draw(out)
d.rounded_rectangle([40 - 9, 40 - 9, 40 + w + 9, 40 + h + 9], radius=86, fill=INK + (255,))
d.rounded_rectangle([40, 40, 40 + w, 40 + h], radius=77, fill=(58, 60, 72, 255)) # the metal frame
d.rounded_rectangle([46, 46, 40 + w - 6, 40 + h - 6], radius=72, fill=(20, 20, 26, 255)) # the glass
d.arc([46, 46, 46 + 160, 46 + 160], 180, 270, fill=(160, 166, 190, 255), width=4) # a catch of light on the frame
for yy in (260, 340): # the buttons on the side
d.rounded_rectangle([40 + w - 2, yy, 40 + w + 8, yy + 60], radius=4, fill=INK + (255,))
mask = Image.new("L", screen.size, 0)
ImageDraw.Draw(mask).rounded_rectangle([0, 0, screen.width, screen.height], radius=52, fill=255)
out.paste(screen, (40 + pad, 40 + pad + 10), mask)
# the reflection of the room on the glass
shine = Image.new("RGBA", out.size, (0, 0, 0, 0))
ImageDraw.Draw(shine).polygon([(40 + pad, 40 + pad + 10), (40 + pad + 330, 40 + pad + 10), (40 + pad, 40 + pad + 640)], fill=(255, 255, 255, 22))
sm = Image.new("L", out.size, 0)
sm.paste(mask, (40 + pad, 40 + pad + 10))
shine.putalpha(ImageChops.multiply(shine.getchannel("A"), sm))
out.alpha_composite(shine)
lens = (60, 230, 120) if camera else (40, 44, 58)
d.ellipse([40 + w // 2 - 15, 40 + 12, 40 + w // 2 + 15, 40 + 42], fill=(10, 10, 14, 255))
d.ellipse([40 + w // 2 - 9, 40 + 18, 40 + w // 2 + 9, 40 + 36], fill=lens + (255,))
return out
# ---------------------------------------------------------------- the scenes
def gradient(top, bottom) -> Image.Image:
arr = np.zeros((H, W, 3), np.uint8)
for c in range(3):
arr[:, :, c] = np.linspace(top[c], bottom[c], H).astype(np.uint8)[:, None]
return Image.fromarray(arr).convert("RGBA")
@lru_cache(maxsize=2)
def room(night: bool) -> Image.Image:
lay = layout()
img = gradient((14, 18, 42), (30, 38, 72)) if night else gradient((255, 248, 232), (250, 226, 196))
d = ImageDraw.Draw(img)
# wallpaper: soft vertical stripes
for x in range(0, W, 90):
d.rectangle([x, 0, x + 38, lay["table"] + 70], fill=(255, 255, 255, 3) if night else (255, 255, 255, 7))
wx, wy, ww, wh = (W // 2 - 280, 80, 560, 500) if not VERT else (W // 2 - 300, 110, 600, 560)
# the window
d.rounded_rectangle([wx - 16, wy - 16, wx + ww + 16, wy + wh + 16], radius=28, fill=INK + (255,))
sky = (14, 20, 54) if night else (176, 222, 255)
d.rounded_rectangle([wx, wy, wx + ww, wy + wh], radius=18, fill=sky + (255,))
if night:
rng = np.random.default_rng(5)
for _ in range(34):
sx, sy = wx + int(rng.uniform(20, ww - 20)), wy + int(rng.uniform(20, wh * 0.8))
r = int(rng.choice([2, 3, 4]))
d.ellipse([sx - r, sy - r, sx + r, sy + r], fill=(240, 240, 255, 255))
d.ellipse([wx + ww - 200, wy + 50, wx + ww - 70, wy + 180], fill=(252, 248, 220, 255))
d.ellipse([wx + ww - 168, wy + 42, wx + ww - 52, wy + 158], fill=sky + (255,))
d.polygon([(wx, wy + wh), (wx + 120, wy + wh - 130), (wx + 260, wy + wh - 60), (wx + 400, wy + wh - 160), (wx + ww, wy + wh - 70), (wx + ww, wy + wh)], fill=(10, 14, 34, 255))
else:
d.ellipse([wx + 70, wy + 60, wx + 230, wy + 220], fill=(255, 214, 90, 255))
for k in range(8):
ang = k * math.pi / 4
d.line([(wx + 150 + math.cos(ang) * 110, wy + 140 + math.sin(ang) * 110), (wx + 150 + math.cos(ang) * 150, wy + 140 + math.sin(ang) * 150)], fill=(255, 224, 120, 255), width=8)
d.polygon([(wx, wy + wh), (wx + 160, wy + wh - 110), (wx + 360, wy + wh - 60), (wx + ww, wy + wh - 130), (wx + ww, wy + wh)], fill=(130, 200, 140, 255))
d.ellipse([wx + 330, wy + 90, wx + 470, wy + 150], fill=(255, 255, 255, 235))
d.ellipse([wx + 380, wy + 70, wx + 520, wy + 140], fill=(255, 255, 255, 235))
d.line([(wx + ww // 2, wy), (wx + ww // 2, wy + wh)], fill=INK + (255,), width=12)
d.line([(wx, wy + wh // 2), (wx + ww, wy + wh // 2)], fill=INK + (255,), width=12)
d.rounded_rectangle([wx - 30, wy + wh + 6, wx + ww + 30, wy + wh + 34], radius=10, fill=(230, 224, 214, 255) if not night else (70, 74, 100, 255), outline=INK + (255,), width=5)
# curtains
cur = (150, 64, 82, 255) if not night else (62, 36, 66, 255)
for sign in (-1, 1):
x0 = wx - 30 if sign < 0 else wx + ww + 30
pts = [(x0, wy - 40), (x0 + sign * 120, wy - 40), (x0 + sign * 90, wy + wh * 0.55), (x0 + sign * 150, wy + wh + 20), (x0, wy + wh + 20)]
d.polygon(pts, fill=cur)
d.line(pts + [pts[0]], fill=INK + (255,), width=6, joint="curve")
for k in range(1, 4):
d.line([(x0 + sign * 30 * k, wy - 36), (x0 + sign * (24 * k + 10), wy + wh)], fill=(0, 0, 0, 40), width=5)
d.line([(wx - 70, wy - 40), (wx + ww + 70, wy - 40)], fill=INK + (255,), width=10)
# the picture of the cat on the wall
fx, fy = (400, 210) if not VERT else (70, 700)
d.rounded_rectangle([fx - 10, fy - 10, fx + 170, fy + 200], radius=10, fill=(120, 80, 52, 255), outline=INK + (255,), width=5)
d.rectangle([fx + 6, fy + 6, fx + 154, fy + 184], fill=(255, 246, 230, 255) if not night else (80, 86, 116, 255))
try:
portrait = R.sprite(4, 0).resize((140, 140), Image.LANCZOS)
if night:
portrait = Image.blend(portrait.convert("RGBA"), Image.new("RGBA", portrait.size, (30, 36, 70, 255)), 0.45)
img.alpha_composite(portrait, (fx + 10, fy + 24))
except Exception:
pass
# the floor with its boards, the table with legs
floor_top = lay["table"] + 70
d.rectangle([0, floor_top, W, H], fill=(26, 30, 50, 255) if night else (206, 160, 118, 255))
for k in range(0, W, 140):
d.line([(k, floor_top), (k - 60, H)], fill=(0, 0, 0, 50), width=4)
d.rectangle([0, floor_top - 14, W, floor_top], fill=(38, 42, 70, 255) if night else (240, 228, 214, 255)) # the skirting board
tx0, tx1 = (W // 2 - 470, W // 2 + 470) if not VERT else (110, W - 110)
for lx in (tx0 + 50, tx1 - 90):
d.rectangle([lx, lay["table"] + 30, lx + 40, floor_top + 40], fill=(86, 60, 44, 255) if night else (150, 100, 66, 255), outline=INK + (255,), width=5)
d.rounded_rectangle([tx0, lay["table"], tx1, lay["table"] + 40], radius=16, fill=(104, 72, 54, 255) if night else (190, 134, 90, 255), outline=INK + (255,), width=6)
d.rounded_rectangle([tx0 + 14, lay["table"] + 4, tx1 - 14, lay["table"] + 14], radius=6, fill=(255, 255, 255, 40))
# light and shade over everything
over = Image.new("RGBA", (W, H), (0, 0, 0, 0))
od = ImageDraw.Draw(over)
if night: # a beam of moonlight from the window to the floor
od.polygon([(wx + 60, wy + wh), (wx + ww - 60, wy + wh), (wx + ww + 260, H), (wx - 200, H)], fill=(160, 190, 255, 26))
else: # and of sun
od.polygon([(wx + 40, wy + wh), (wx + ww - 40, wy + wh), (wx + ww + 320, H), (wx - 260, H)], fill=(255, 236, 160, 52))
img.alpha_composite(over.filter(ImageFilter.GaussianBlur(18)))
return img
@lru_cache(maxsize=1)
def vignette() -> Image.Image:
y, x = np.ogrid[:H, :W]
d = np.sqrt(((x - W / 2) / (W / 2)) ** 2 + ((y - H / 2) / (H / 2)) ** 2)
alpha = np.clip((d - 0.65) * 120, 0, 90).astype(np.uint8)
out = Image.new("RGBA", (W, H), (0, 0, 20, 0))
out.putalpha(Image.fromarray(alpha))
return out
@lru_cache(maxsize=1)
def contact_shadow() -> Image.Image:
layer = Image.new("RGBA", (500, 120), (0, 0, 0, 0))
ImageDraw.Draw(layer).ellipse([60, 40, 440, 90], fill=(10, 10, 30, 120))
return layer.filter(ImageFilter.GaussianBlur(12))
def glow(img, x, y, size, alpha):
layer = Image.new("RGBA", (size * 2, size * 2), (0, 0, 0, 0))
ImageDraw.Draw(layer).ellipse([size // 2, size // 2, size * 3 // 2, size * 3 // 2], fill=(150, 190, 255, int(110 * alpha)))
put_c(img, layer.filter(ImageFilter.GaussianBlur(size // 4)), x, y)
def zoom_at(t: float) -> float:
return in_out(seg(t, 11.3, 12.3)) * (1 - in_out(seg(t, 14.4, 14.9)))
def phone_state(t: float):
"""(screen key, camera dot, x, y, angle, scale) of the phone at time t."""
lay = layout()
px, py = lay["phone"]
base = lay["base"]
to_thief = in_out(seg(t, 2.5, 3.0)) * (1 - in_out(seg(t, 5.4, 5.9)))
to_guy = in_out(seg(t, 9.5, 10.0)) * (1 - in_out(seg(t, 14.6, 15.2)))
dx = to_thief * (lay["thief_x"] + 250 - px) + to_guy * (lay["guy_x"] - 250 - px)
dy = -80 * to_thief - 60 * to_guy
angle = -7 * to_thief + 7 * to_guy
camera = False
state = "wait"
if 3.4 <= t < 3.9 or 4.6 <= t < 5.1:
state = "wrong"
camera = True
dx += math.sin(t * 60) * 6
if 10.2 <= t < 10.9:
state = "right"
key = ("lock", int(t * 10), state)
if t >= 10.9:
key = ("journal", int(10 * (0.5 + 0.5 * math.sin((t - 11.5) * 5))) if t > 12.4 else 0, "")
zoom = zoom_at(t)
scale = lerp(base, lay["zoom"], zoom)
cx = lerp(px + dx, W // 2, zoom)
cy = lerp(py + dy, H // 2, zoom)
return key, camera, cx, cy, angle * (1 - zoom), scale
def blinks(t: float) -> bool:
"""A blink every few seconds."""
return (t * 0.9) % 3.0 < 0.13
def draw_people(img, t):
lay = layout()
size = lay["person"]
feet = lay["feet"]
top = feet - 700 * size
key, camera, pcx, pcy, pang, pscale = phone_state(t)
# the thief: comes in from the left, reaches, takes the phone, fails, puts it back, goes away
tx = lay["thief_x"]
walking = (0.5 < t < 2.3) or (6.0 < t < 7.5)
x = lerp(-260, tx, out_cubic(seg(t, 0.5, 2.3))) if t < 3.0 else lerp(tx, -320, in_out(seg(t, 6.0, 7.5)))
step = t * 9 if walking else 0.0
arm = None
mood = "sneak"
if 2.2 <= t < 5.9:
reach = out_cubic(seg(t, 2.2, 2.7)) * (1 - seg(t, 5.5, 5.9))
hx, hy = pcx - HALF * pscale + 14, pcy + 70
arm = (lerp(318, 200 + (hx - x) / size, reach), lerp(478, 330 + (hy - top) / size, reach))
mood = "angry" if t > 5.0 else "sneak"
if t < T_DAY + 0.4:
layer = person("thief", step, arm, mood, size=size, blink=blinks(t))
bob = abs(math.sin(step)) * 8 if walking else 0
put_c(img, contact_shadow(), x + 10, feet - 4, size * 0.62, 0.9)
put_c(img, layer, x, feet - layer.height / 2 - bob, 1.0, 1.0)
# the owner: comes in from the right in the morning, opens the phone with his finger, is struck by what he sees
gx = lay["guy_x"]
if t >= 8.0:
xg = lerp(W + 260, gx, out_cubic(seg(t, 8.0, 9.4)))
walking = t < 9.4
step = t * 9 if walking else 0.0
mood = "shock" if t >= 12.4 else "calm"
arm = None
if 9.4 <= t < 14.9:
reach = out_cubic(seg(t, 9.4, 9.9)) * (1 - zoom_at(t))
hx, hy = pcx + HALF * pscale - 14, pcy + 70
arm = (lerp(318, 200 - (hx - xg) / size, reach), lerp(478, 330 + (hy - top) / size, reach))
layer = person("guy", step, arm, mood, look=(-1, 0), size=size, blink=blinks(t)).transpose(Image.FLIP_LEFT_RIGHT)
bob = abs(math.sin(step)) * 8 if walking else 0
put_c(img, contact_shadow(), xg - 10, feet - 4, size * 0.62, 0.9)
put_c(img, layer, xg, feet - layer.height / 2 - bob, 1.0, 1.0)
if t >= 12.5: # the "!" over the head
k = out_back(seg(t, 12.5, 12.9), 2.0)
put_c(img, bang(), xg, top - 20, k * size, clamp(k * 2) * (1 - seg(t, 14.6, 14.9)))
@lru_cache(maxsize=1)
def bang() -> Image.Image:
layer = Image.new("RGBA", (200, 200), (0, 0, 0, 0))
d = ImageDraw.Draw(layer)
d.ellipse([10, 10, 190, 190], fill=INK + (255,))
d.ellipse([20, 20, 180, 180], fill=(255, 214, 90, 255))
d.text((100, 104), "!", font=font(130, "black"), fill=INK + (255,), anchor="mm")
return layer
def clock(img, t):
text = "03:12" if t < T_DAY else "08:30"
word = "Ночью" if t < T_DAY else "Утром"
ink = (230, 236, 255) if t < T_DAY else INK
x = 90 if not VERT else 80
plate = Image.new("RGBA", (330, 170), (0, 0, 0, 0))
ImageDraw.Draw(plate).rounded_rectangle([0, 0, 329, 169], radius=36, fill=(10, 14, 34, 150) if t < T_DAY else (255, 255, 255, 170))
img.alpha_composite(plate, (x - 28, 22))
V.text(img, (x + 8, 70), word, 44, "semi", ink, "lm", alpha=out_cubic(seg(t % T_DAY if t < T_DAY else t - T_DAY, 0.2, 0.8)))
V.text(img, (x + 8, 130), text, 72, "black", ink, "lm", alpha=out_cubic(seg(t % T_DAY if t < T_DAY else t - T_DAY, 0.3, 0.9)))
def scene(t: float) -> Image.Image:
lay = layout()
night = room(True)
day = room(False)
k = in_out(seg(t, T_DAY - 0.4, T_DAY + 0.3))
img = night.copy() if k <= 0 else (day.copy() if k >= 1 else Image.blend(night, day, k))
key, camera, cx, cy, angle, scale = phone_state(t)
if t < T_DAY:
glow(img, cx, cy, 700, 1.0)
draw_people(img, t)
held = abs(cy - lay["phone"][1]) > 6 or abs(cx - lay["phone"][0]) > 6
if not held and scale < lay["base"] * 1.2:
put_c(img, contact_shadow(), cx, lay["table"] + 6, scale * 1.5, 0.8)
put_c(img, phone(key, camera), cx, cy, scale, 1.0, angle)
img.alpha_composite(vignette())
clock(img, t)
return img
def frame_at(t: float) -> Image.Image:
if t < T_CAT - 0.35:
img = scene(t)
else:
cat = R.frame_at(max(0.25, t - CAT_OFFSET), CAT_LINES).convert("RGBA")
k = in_out(seg(t, T_CAT - 0.35, T_CAT + 0.25))
img = cat if k >= 1 else Image.blend(scene(min(t, T_CAT)), cat, k)
fade = clamp(t / 0.35)
if fade < 1:
img.alpha_composite(Image.new("RGBA", img.size, (0, 0, 0, int(255 * (1 - fade)))))
return img.convert("RGB").convert("RGBA")
# ---------------------------------------------------------------- the sound
def thud(rng):
t = M.tt(0.07)
return M.lp(rng.standard_normal(len(t)), 420) * np.exp(-t * 55) * 1.4
def buzzer():
t = M.tt(0.3)
f = 190 - 40 * (t / 0.3)
s = np.sign(np.sin(2 * math.pi * np.cumsum(f) / M.SR)) * 0.5
return M.lp(s, 1800) * np.minimum(1, t / 0.01) * np.clip((0.3 - t) / 0.08, 0, 1)
def soundtrack(duration: float, path: Path) -> None:
rng = np.random.default_rng(11)
mix = M.Mix(duration + 0.5)
for k in range(int(T_DAY)): # the clock ticks in the night
mix.add(M.tick(1500), 0.4 + k * 1.0, 0.07, pan=-0.3)
for t0 in np.arange(0.7, 2.3, 0.42): # the thief comes in
mix.add(thud(rng), float(t0), 0.5, pan=-0.5)
for t0 in np.arange(6.1, 7.5, 0.42): # and goes away
mix.add(thud(rng), float(t0), 0.45, pan=-0.5)
mix.add(M.whoosh(0.4, True), 2.55, 0.25, pan=-0.3)
mix.add(M.pop(700, 250, 0.08), 2.9, 0.3, pan=-0.3)
for t0 in (3.4, 4.6): # the wrong finger, and the camera
mix.add(buzzer(), t0, 0.5)
mix.add(M.tick(2600), t0 + 0.15, 0.5, pan=0.4)
mix.add(M.pop(500, 200, 0.08), 5.75, 0.3, pan=-0.3)
for t0 in np.arange(8.2, 9.4, 0.42): # the owner comes in
mix.add(thud(rng), float(t0), 0.45, pan=0.5)
mix.add(M.whoosh(0.4, True), 9.55, 0.25, pan=0.3)
mix.add(M.bell(M.midi(88), 1.4), 10.6, 0.4, send=0.4) # the right finger
mix.add(M.sparkle(1.0, 6, 88, 3), 10.65, 0.35, send=0.4)
mix.add(M.whoosh(0.5, True), 11.3, 0.3)
mix.add(M.boing(), 12.55, 0.5)
mix.add(M.pop(1000, 300, 0.12), 12.6, 0.4)
for i, (t0, k, n) in enumerate(R.meow_times(CAT_LINES)):
base = [560.0, 610.0, 520.0, 590.0, 640.0][i % 5] * (1.0 - 0.07 * k)
length = R.MEOW * (1.12 if k == n - 1 and n > 1 else 1.0)
mix.add(R.meow(base, length, seed=i), CAT_OFFSET + t0, 0.9, pan=-0.2 + 0.1 * (k % 2), send=0.1)
end = CAT_OFFSET + R.starts(CAT_LINES)[-2]
mix.add(M.sparkle(1.6, 12, 84, 2), end + 0.9, 0.55, send=0.4)
mix.add(M.bell(M.midi(93), 1.8), end + 0.9, 0.3, send=0.5)
data = np.stack([mix.left, mix.right], axis=1)[: int((duration + 0.5) * M.SR)]
data = np.tanh(data * 1.1)
data /= max(1e-9, np.abs(data).max()) / 0.85
M.write(path, data.astype(np.float32))
# ---------------------------------------------------------------- the video
def duration() -> float:
return CAT_OFFSET + R.total(CAT_LINES)
def _init(vertical: bool) -> None:
setup(vertical)
R.prepare()
def render_frame(index: int) -> bytes:
return frame_at(index / FPS).convert("RGB").tobytes()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--vertical", action="store_true")
parser.add_argument("--still", type=float, nargs="*")
parser.add_argument("--no-music", action="store_true")
parser.add_argument("--jobs", type=int, default=6)
args = parser.parse_args()
setup(args.vertical)
OUT.mkdir(parents=True, exist_ok=True)
R.prepare()
tag = "_9x16" if args.vertical else ""
if args.still:
for t in args.still:
path = OUT / ("still%s_%05.2f.png" % (tag, t))
frame_at(t).convert("RGB").save(path)
print(path)
return
total = duration()
wav = OUT / "sound.wav"
soundtrack(total, wav)
out = OUT / ("thief%s.mp4" % tag)
music = ROOT / "work" / "music" / "store.mp3"
command = [shutil.which("ffmpeg") or "ffmpeg", "-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgb24", "-s", "%dx%d" % (W, H),
"-r", str(FPS), "-i", "-", "-i", str(wav)]
if args.no_music or not music.is_file():
command += ["-af", "afade=t=out:st=%.2f:d=0.6" % (total - 0.6)]
else:
command += ["-i", str(music), "-filter_complex",
"[2:a]adelay=%d|%d,volume=0.2,afade=t=in:st=%.2f:d=1.0,afade=t=out:st=%.2f:d=1.0[b];[1:a][b]amix=inputs=2:duration=first:normalize=0,"
"afade=t=out:st=%.2f:d=0.6[a]" % (int(T_DAY * 1000), int(T_DAY * 1000), T_DAY, total - 1.0, total - 0.6), "-map", "0:v", "-map", "[a]"]
command += ["-c:v", "libx264", "-preset", "medium", "-crf", "17", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "192k", "-t", "%.2f" % total, str(out)]
proc = subprocess.Popen(command, stdin=subprocess.PIPE)
frames = int(round(total * FPS))
with Pool(args.jobs, initializer=_init, initargs=(args.vertical,)) as pool:
for n, data in enumerate(pool.imap(render_frame, range(frames), chunksize=4)):
proc.stdin.write(data)
if n % 60 == 0:
print("frame %d/%d" % (n, frames), flush=True)
proc.stdin.close()
proc.wait()
print("done:", out)
if __name__ == "__main__":
main()