A pair of people can have a kitten, a puppy or a lion cub in place of TikTok's streak creature: it grows with the days of the streak (cub, teen, adult, elder), goes grey while the streak is about to burn out, can be dragged in the chat and is also shown on the big streak screen. An animal is given by a link (lihont.space/pet) that the friend accepts. The drawings are made by scripts/make_pet_designs.py and served as strips of frames. Also here: the promo video scripts (make_pet_video.py on top of make_store_video.py), a retry pause for strips that failed to load, stricter checks in the answer to a gift. Work in progress: the gift service is not switched on for everyone yet and there is no public build with the animals. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
837 lines
41 KiB
Python
837 lines
41 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
LihonT Store: a promo video in the style of OpenAI's product videos (white ground, one pill bar that types, rounded picture tiles
|
||
with soft shadows that glide, fan out and zoom, confetti, glossy "candy" letters, the logo at the end). The scenes are laid on the strong
|
||
beats of a song (work/music/<song>.mp3 and <song>.beats.json, made by scripts/analyze_beats.py).
|
||
|
||
Only our own material is shown: avatars with frames drawn by scripts/frame_render.py (work/serena-promo/beat-cache), pictures drawn here,
|
||
invented names. The style is only a reference: nothing of another video is copied.
|
||
|
||
The key scene: a cursor drags a picture into a window, it uploads, a check is drawn, the window slides down and an avatar appears with
|
||
that very picture cut into a ring as its frame.
|
||
|
||
py -X utf8 -B scripts/make_store_video.py # work/store-video/store_openai.mp4
|
||
py -X utf8 -B scripts/make_store_video.py --still 7.5 # one frame as a picture
|
||
py -X utf8 -B scripts/make_store_video.py --sheet # a contact sheet of the whole video
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import json
|
||
import math
|
||
import random
|
||
import shutil
|
||
import subprocess
|
||
from functools import lru_cache
|
||
from multiprocessing import Pool
|
||
from pathlib import Path
|
||
|
||
from PIL import Image, ImageChops, ImageDraw, ImageFilter, ImageFont
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
OUT_DIR = ROOT / "work" / "store-video"
|
||
CACHE = ROOT / "work" / "serena-promo" / "beat-cache"
|
||
W, H, FPS = 1920, 1080, 30
|
||
VERT = False
|
||
CX, CY = W // 2, H // 2
|
||
|
||
|
||
def set_format(vertical: bool) -> None:
|
||
"""Landscape 1920x1080 (the default) or portrait 1080x1920: the scenes read the size and the middle from here."""
|
||
global W, H, VERT, CX, CY
|
||
VERT = bool(vertical)
|
||
W, H = (1080, 1920) if VERT else (1920, 1080)
|
||
CX, CY = W // 2, H // 2
|
||
|
||
INK = (24, 24, 28)
|
||
GREY = (118, 118, 126)
|
||
SOFT = (240, 240, 243)
|
||
VIOLET = (122, 79, 208)
|
||
VIOLET_DARK = (92, 56, 168)
|
||
GOLD = (238, 190, 107)
|
||
PASTELS = [(255, 226, 214), (222, 232, 255), (226, 247, 232), (255, 240, 200), (240, 224, 255), (255, 224, 240)]
|
||
FLOWERS = [(255, 120, 160), (255, 170, 80), (120, 190, 255), (160, 120, 255), (90, 210, 150), (255, 210, 90)]
|
||
|
||
FONTS = {"reg": "segoeui.ttf", "semi": "seguisb.ttf", "bold": "segoeuib.ttf", "black": "seguibl.ttf"}
|
||
|
||
# filled by configure()
|
||
SONG = None
|
||
S: list = []
|
||
DURATION = 0.0
|
||
FRAMES = 0
|
||
|
||
|
||
def configure(name: str) -> None:
|
||
global SONG, S, DURATION, FRAMES
|
||
SONG = ROOT / "work" / "music" / (name + ".mp3")
|
||
data = json.loads(SONG.with_suffix(".beats.json").read_text(encoding="utf-8"))
|
||
S = data["strong_beats"]
|
||
DURATION = round(data["duration"], 2)
|
||
FRAMES = int(round(DURATION * FPS))
|
||
|
||
|
||
# ---------------------------------------------------------------- maths and easing
|
||
|
||
def clamp(x, lo=0.0, hi=1.0):
|
||
return max(lo, min(hi, x))
|
||
|
||
|
||
def lerp(a, b, k):
|
||
return a + (b - a) * k
|
||
|
||
|
||
def seg(t, a, b):
|
||
return clamp((t - a) / (b - a)) if b > a else (1.0 if t >= a else 0.0)
|
||
|
||
|
||
def out_cubic(x):
|
||
return 1 - (1 - clamp(x)) ** 3
|
||
|
||
|
||
def in_cubic(x):
|
||
return clamp(x) ** 3
|
||
|
||
|
||
def in_out(x):
|
||
x = clamp(x)
|
||
return 4 * x ** 3 if x < 0.5 else 1 - (-2 * x + 2) ** 3 / 2
|
||
|
||
|
||
def out_back(x, s=1.7):
|
||
x = clamp(x)
|
||
return 1 + (s + 1) * (x - 1) ** 3 + s * (x - 1) ** 2
|
||
|
||
|
||
def out_expo(x):
|
||
x = clamp(x)
|
||
return 1.0 if x >= 1 else 1 - 2 ** (-10 * x)
|
||
|
||
|
||
# ---------------------------------------------------------------- drawing helpers
|
||
|
||
@lru_cache(maxsize=64)
|
||
def font(size: int, weight: str = "reg") -> ImageFont.FreeTypeFont:
|
||
return ImageFont.truetype("C:/Windows/Fonts/" + FONTS[weight], int(size))
|
||
|
||
|
||
def put(img, sprite, x, y, alpha=1.0):
|
||
"""Composites a sprite with its top-left corner at (x, y), clipped to the picture, with an overall opacity."""
|
||
x, y = int(round(x)), int(round(y))
|
||
if alpha < 0.999:
|
||
sprite = sprite.copy()
|
||
a = sprite.getchannel("A").point(lambda v: int(v * alpha))
|
||
sprite.putalpha(a)
|
||
left, top = max(0, -x), max(0, -y)
|
||
right, bottom = min(sprite.width, img.width - x), min(sprite.height, img.height - y)
|
||
if right <= left or bottom <= top:
|
||
return
|
||
img.alpha_composite(sprite.crop((left, top, right, bottom)), (x + left, y + top))
|
||
|
||
|
||
def put_c(img, sprite, cx, cy, scale=1.0, alpha=1.0, angle=0.0):
|
||
"""The same with the sprite's centre at (cx, cy), optionally scaled and turned."""
|
||
if scale <= 0.01 or alpha <= 0.003:
|
||
return
|
||
if abs(scale - 1.0) > 0.003:
|
||
sprite = sprite.resize((max(1, int(sprite.width * scale)), max(1, int(sprite.height * scale))), Image.Resampling.BICUBIC)
|
||
if abs(angle) > 0.05:
|
||
sprite = sprite.rotate(angle, resample=Image.Resampling.BICUBIC, expand=True)
|
||
put(img, sprite, cx - sprite.width / 2, cy - sprite.height / 2, alpha)
|
||
|
||
|
||
def text(img, xy, s, size, weight="reg", fill=INK, anchor="mm", alpha=1.0):
|
||
if alpha <= 0.003 or not s:
|
||
return
|
||
f = font(size, weight)
|
||
box = ImageDraw.Draw(Image.new("L", (1, 1))).textbbox((0, 0), s, font=f, anchor="lt")
|
||
pad = 6
|
||
layer = Image.new("RGBA", (box[2] + 2 * pad, box[3] + 2 * pad + 8), (0, 0, 0, 0))
|
||
ImageDraw.Draw(layer).text((pad, pad), s, font=f, fill=tuple(fill) + (255,), anchor="lt")
|
||
w, h = layer.size
|
||
x, y = xy
|
||
x -= {"l": 0, "m": w / 2, "r": w}[anchor[0]] - (0 if anchor[0] == "l" else 0)
|
||
y -= {"t": 0, "m": h / 2, "b": h}[anchor[1]] if anchor[1] in "tmb" else h / 2
|
||
put(img, layer, x - (pad if anchor[0] == "l" else 0), y, alpha)
|
||
|
||
|
||
@lru_cache(maxsize=512)
|
||
def rr(w, h, r, fill, outline=None, ow=0):
|
||
"""An anti-aliased rounded rectangle (drawn three times as large and scaled down)."""
|
||
k = 3
|
||
layer = Image.new("RGBA", (w * k, h * k), (0, 0, 0, 0))
|
||
d = ImageDraw.Draw(layer)
|
||
d.rounded_rectangle([0, 0, w * k - 1, h * k - 1], radius=r * k, fill=tuple(fill) + (255,) if fill else None,
|
||
outline=tuple(outline) + (255,) if outline else None, width=ow * k)
|
||
return layer.resize((w, h), Image.Resampling.LANCZOS)
|
||
|
||
|
||
@lru_cache(maxsize=256)
|
||
def disc(d, fill):
|
||
k = 3
|
||
layer = Image.new("RGBA", (d * k, d * k), (0, 0, 0, 0))
|
||
ImageDraw.Draw(layer).ellipse([0, 0, d * k - 1, d * k - 1], fill=tuple(fill) + (255,))
|
||
return layer.resize((d, d), Image.Resampling.LANCZOS)
|
||
|
||
|
||
@lru_cache(maxsize=128)
|
||
def card(w, h, r, fill=(255, 255, 255), blur=34, dy=16, alpha=48):
|
||
"""A rounded card with a soft shadow; the sprite is larger than the card by the shadow's reach."""
|
||
pad = blur * 2
|
||
layer = Image.new("RGBA", (w + 2 * pad, h + 2 * pad), (0, 0, 0, 0))
|
||
shadow = Image.new("RGBA", layer.size, (0, 0, 0, 0))
|
||
ImageDraw.Draw(shadow).rounded_rectangle([pad, pad + dy, pad + w, pad + h + dy], radius=r, fill=(20, 20, 40, alpha))
|
||
layer.alpha_composite(shadow.filter(ImageFilter.GaussianBlur(blur / 2)))
|
||
layer.alpha_composite(rr(w, h, r, fill), (pad, pad))
|
||
return layer
|
||
|
||
|
||
@lru_cache(maxsize=64)
|
||
def soft_shadow(w, h, r, blur=40, dy=22, alpha=70):
|
||
pad = int(blur) * 2
|
||
shadow = Image.new("RGBA", (w + 2 * pad, h + 2 * pad), (0, 0, 0, 0))
|
||
ImageDraw.Draw(shadow).rounded_rectangle([pad, pad + dy, pad + w, pad + h + dy], radius=r, fill=(20, 20, 40, alpha))
|
||
return shadow.filter(ImageFilter.GaussianBlur(blur / 2))
|
||
|
||
|
||
def mask_round(img: Image.Image, r: int) -> Image.Image:
|
||
k = 3
|
||
m = Image.new("L", (img.width * k, img.height * k), 0)
|
||
ImageDraw.Draw(m).rounded_rectangle([0, 0, img.width * k - 1, img.height * k - 1], radius=r * k, fill=255)
|
||
out = img.convert("RGBA")
|
||
out.putalpha(ImageChops.multiply(out.getchannel("A"), m.resize(img.size, Image.Resampling.LANCZOS)))
|
||
return out
|
||
|
||
|
||
# ---------------------------------------------------------------- pictures drawn here
|
||
|
||
@lru_cache(maxsize=16)
|
||
def sunset(size: int) -> Image.Image:
|
||
"""The picture that is dragged in: a sunset with hills and a flower, made of nothing but gradients."""
|
||
img = Image.new("RGB", (size, size))
|
||
px = img.load()
|
||
top, mid, low = (96, 82, 214), (255, 112, 140), (255, 190, 100)
|
||
for y in range(size):
|
||
v = y / size
|
||
c = tuple(int(lerp(top[i], mid[i], v / 0.6)) for i in range(3)) if v < 0.6 else tuple(int(lerp(mid[i], low[i], (v - 0.6) / 0.4)) for i in range(3))
|
||
for x in range(size):
|
||
px[x, y] = c
|
||
d = ImageDraw.Draw(img, "RGBA")
|
||
sun = size * 0.18
|
||
d.ellipse([size * 0.5 - sun, size * 0.52 - sun, size * 0.5 + sun, size * 0.52 + sun], fill=(255, 244, 200, 255))
|
||
for i, (col, base, amp) in enumerate([((120, 70, 170), 0.70, 0.07), ((78, 44, 132), 0.80, 0.06), ((44, 26, 90), 0.90, 0.05)]):
|
||
pts = [(0, size)]
|
||
for x in range(0, size + 1, 8):
|
||
pts.append((x, size * base + math.sin(x / size * (3 + i) + i) * size * amp))
|
||
pts.append((size, size))
|
||
d.polygon(pts, fill=col + (255,))
|
||
for n in range(14): # petals over the first hill: a flower in the corner
|
||
a = n / 14 * math.tau
|
||
cx, cy = size * 0.22 + math.cos(a) * size * 0.07, size * 0.74 + math.sin(a) * size * 0.07
|
||
d.ellipse([cx - size * 0.035, cy - size * 0.035, cx + size * 0.035, cy + size * 0.035], fill=(255, 232, 240, 255))
|
||
d.ellipse([size * 0.22 - size * 0.03, size * 0.74 - size * 0.03, size * 0.22 + size * 0.03, size * 0.74 + size * 0.03], fill=(255, 190, 70, 255))
|
||
return img.convert("RGBA")
|
||
|
||
|
||
@lru_cache(maxsize=16)
|
||
def ring_of(size: int) -> Image.Image:
|
||
"""The sunset cut into a ring, as the app does: the avatar fills the middle 1/1.3 of the picture."""
|
||
art = sunset(size).copy()
|
||
k = 3
|
||
m = Image.new("L", (size * k, size * k), 0)
|
||
dm = ImageDraw.Draw(m)
|
||
outer, inner = size * k / 2 - k, size * k / 1.3 / 2 * 0.96
|
||
dm.ellipse([size * k / 2 - outer, size * k / 2 - outer, size * k / 2 + outer, size * k / 2 + outer], fill=255)
|
||
dm.ellipse([size * k / 2 - inner, size * k / 2 - inner, size * k / 2 + inner, size * k / 2 + inner], fill=0)
|
||
art.putalpha(m.resize((size, size), Image.Resampling.LANCZOS))
|
||
return art
|
||
|
||
|
||
@lru_cache(maxsize=32)
|
||
def avatar(d: int) -> Image.Image:
|
||
"""A plain avatar: a soft disc with a person in it."""
|
||
k = 3
|
||
layer = Image.new("RGBA", (d * k, d * k), (0, 0, 0, 0))
|
||
g = ImageDraw.Draw(layer)
|
||
g.ellipse([0, 0, d * k - 1, d * k - 1], fill=(228, 224, 238, 255))
|
||
g.ellipse([d * k * 0.34, d * k * 0.20, d * k * 0.66, d * k * 0.52], fill=(255, 255, 255, 255))
|
||
g.ellipse([d * k * 0.18, d * k * 0.56, d * k * 0.82, d * k * 1.18], fill=(255, 255, 255, 255))
|
||
clip = Image.new("L", layer.size, 0)
|
||
ImageDraw.Draw(clip).ellipse([0, 0, d * k - 1, d * k - 1], fill=255)
|
||
layer.putalpha(ImageChops.multiply(layer.getchannel("A"), clip))
|
||
return layer.resize((d, d), Image.Resampling.LANCZOS)
|
||
|
||
|
||
ART_NAMES = ["basic.cat", "cat-on-flower", "basic.crown", "cat-on-heart", "basic.flame", "cat-on-hexagon", "basic.flower", "cat-on-squircle",
|
||
"basic.helmet", "cat-on-triangle"]
|
||
|
||
|
||
@lru_cache(maxsize=32)
|
||
def art(name: str) -> Image.Image:
|
||
return Image.open(CACHE / (name + ".png")).convert("RGBA")
|
||
|
||
|
||
@lru_cache(maxsize=128)
|
||
def tile(index: int, size: int, cat: bool = False) -> Image.Image:
|
||
"""A rounded picture tile: a pastel ground with an avatar in a frame (our own art), like the pictures of a product video.
|
||
The art is cut to its real outline and fitted with a margin, so that petals, ears and crowns are never cropped by the tile.
|
||
With `cat` the tile of the same colour shows the cat frame (what the tiles turn into when they gather)."""
|
||
name = "basic.cat" if cat else ART_NAMES[index % len(ART_NAMES)]
|
||
source = art(name)
|
||
shown = source.crop(source.getchannel("A").getbbox())
|
||
k = size * 0.78 / max(shown.size)
|
||
shown = shown.resize((max(1, int(shown.width * k)), max(1, int(shown.height * k))), Image.Resampling.LANCZOS)
|
||
ground = Image.new("RGBA", (size, size), PASTELS[index % len(PASTELS)] + (255,))
|
||
ground.alpha_composite(shown, ((size - shown.width) // 2, (size - shown.height) // 2))
|
||
return mask_round(ground, int(size * 0.1))
|
||
|
||
|
||
@lru_cache(maxsize=32)
|
||
def bubble_picture(index: int, w: int, h: int) -> Image.Image:
|
||
"""Chat-bubble backgrounds drawn from gradients, stripes and stars."""
|
||
rng = random.Random(index * 77 + 5)
|
||
pairs = [((255, 140, 190), (160, 110, 255)), ((90, 210, 160), (60, 150, 230)), ((255, 190, 90), (255, 110, 130)), ((110, 170, 255), (190, 130, 255))]
|
||
c1, c2 = pairs[index % len(pairs)]
|
||
img = Image.new("RGBA", (w, h))
|
||
px = img.load()
|
||
for y in range(h):
|
||
for x in range(w):
|
||
v = (x / w * 0.6 + y / h * 0.4)
|
||
px[x, y] = tuple(int(lerp(c1[i], c2[i], v)) for i in range(3)) + (255,)
|
||
d = ImageDraw.Draw(img, "RGBA")
|
||
kind = index % 4
|
||
for _ in range(14):
|
||
x, y, r = rng.randint(0, w), rng.randint(0, h), rng.randint(4, 14)
|
||
if kind == 0:
|
||
d.ellipse([x - r, y - r, x + r, y + r], fill=(255, 255, 255, 46))
|
||
elif kind == 1:
|
||
d.line([x - 30, y + 30, x + 30, y - 30], fill=(255, 255, 255, 34), width=6)
|
||
elif kind == 2 and rng.random() < 0.45:
|
||
d.polygon([(x, y - r * 1.6), (x + r * 0.5, y - r * 0.4), (x + r * 1.6, y), (x + r * 0.5, y + r * 0.4), (x, y + r * 1.6), (x - r * 0.5, y + r * 0.4), (x - r * 1.6, y), (x - r * 0.5, y - r * 0.4)], fill=(255, 255, 255, 90))
|
||
elif kind == 3:
|
||
d.arc([x - 40, y - 20, x + 40, y + 20], 0, 180, fill=(255, 255, 255, 50), width=5)
|
||
return img
|
||
|
||
|
||
# ---------------------------------------------------------------- small pieces of interface
|
||
|
||
@lru_cache(maxsize=8)
|
||
def cursor_sprite() -> Image.Image:
|
||
k = 4
|
||
pts = [(0, 0), (0, 29), (8, 22), (14, 35), (20, 32), (14, 20), (25, 20)]
|
||
big = Image.new("RGBA", (40 * k, 48 * k), (0, 0, 0, 0))
|
||
d = ImageDraw.Draw(big)
|
||
d.polygon([(x * k + 6 * k, y * k + 4 * k) for x, y in pts], fill=(255, 255, 255, 255), outline=(255, 255, 255, 255), width=5 * k)
|
||
d.polygon([(x * k + 6 * k, y * k + 4 * k) for x, y in pts], fill=(18, 18, 20, 255))
|
||
out = big.resize((40, 48), Image.Resampling.LANCZOS)
|
||
sh = Image.new("RGBA", (64, 72), (0, 0, 0, 0))
|
||
sh.alpha_composite(out.filter(ImageFilter.GaussianBlur(3)).point(lambda v: v * 0.5 if v < 255 else v), (10, 14))
|
||
sh.alpha_composite(out, (8, 8))
|
||
return sh
|
||
|
||
|
||
def check_path(cx, cy, size, k):
|
||
"""Points of a check mark drawn from its start to k (0..1) of its length."""
|
||
p = [(cx - size * 0.5, cy + size * 0.02), (cx - size * 0.14, cy + size * 0.38), (cx + size * 0.52, cy - size * 0.36)]
|
||
lengths = [math.dist(p[0], p[1]), math.dist(p[1], p[2])]
|
||
run = k * sum(lengths)
|
||
out = [p[0]]
|
||
for i in range(2):
|
||
if run >= lengths[i]:
|
||
out.append(p[i + 1])
|
||
run -= lengths[i]
|
||
else:
|
||
f = run / lengths[i]
|
||
out.append((lerp(p[i][0], p[i + 1][0], f), lerp(p[i][1], p[i + 1][1], f)))
|
||
break
|
||
return out
|
||
|
||
|
||
def draw_check(img, cx, cy, size, k, color=(255, 255, 255), width=18):
|
||
big = Image.new("RGBA", (int(size * 1.6) * 3, int(size * 1.6) * 3), (0, 0, 0, 0))
|
||
d = ImageDraw.Draw(big)
|
||
ox, oy = big.width / 2, big.height / 2
|
||
pts = [((x - cx) * 3 + ox, (y - cy) * 3 + oy) for x, y in check_path(cx, cy, size, k)]
|
||
d.line(pts, fill=tuple(color) + (255,), width=width * 3, joint="curve")
|
||
for x, y in (pts[0], pts[-1]):
|
||
d.ellipse([x - width * 1.5, y - width * 1.5, x + width * 1.5, y + width * 1.5], fill=tuple(color) + (255,))
|
||
small = big.resize((big.width // 3, big.height // 3), Image.Resampling.LANCZOS)
|
||
put(img, small, cx - small.width / 2, cy - small.height / 2)
|
||
|
||
|
||
def burst(img, cx, cy, t, t0, seed=1, n=40, power=520, life=1.1, colors=None):
|
||
"""Confetti: little circles and flowers thrown from a point, falling back."""
|
||
rng = random.Random(seed)
|
||
colors = colors or FLOWERS
|
||
age = t - t0
|
||
if age < 0 or age > life:
|
||
return
|
||
for i in range(n):
|
||
a = rng.uniform(0, math.tau)
|
||
v = rng.uniform(0.35, 1.0) * power
|
||
size = rng.randint(8, 20)
|
||
col = colors[i % len(colors)]
|
||
x = cx + math.cos(a) * v * out_cubic(age / life) * 0.9
|
||
y = cy + math.sin(a) * v * out_cubic(age / life) * 0.9 + 380 * age * age
|
||
k = 1 - clamp(age / life) ** 2
|
||
if i % 3 == 0:
|
||
for p in range(5):
|
||
pa = p / 5 * math.tau + age * 3
|
||
put_c(img, disc(size, col), x + math.cos(pa) * size * 0.7, y + math.sin(pa) * size * 0.7, 1.0, k)
|
||
put_c(img, disc(size, (255, 235, 150)), x, y, 0.8, k)
|
||
else:
|
||
put_c(img, disc(size, col), x, y, 1.0, k)
|
||
|
||
|
||
def ripple(img, cx, cy, t, t0, color=(120, 80, 210), maxr=70):
|
||
age = t - t0
|
||
if 0 <= age <= 0.5:
|
||
k = age / 0.5
|
||
d = int(maxr * 2 * out_cubic(k))
|
||
if d > 4:
|
||
layer = Image.new("RGBA", (d + 8, d + 8), (0, 0, 0, 0))
|
||
ImageDraw.Draw(layer).ellipse([4, 4, d + 4, d + 4], outline=tuple(color) + (int(180 * (1 - k)),), width=6)
|
||
put(img, layer, cx - layer.width / 2, cy - layer.height / 2)
|
||
|
||
|
||
def pill(img, cx, cy, s, fill=(32, 32, 36), color=(255, 255, 255), size=26, alpha=1.0, scale=1.0, weight="semi"):
|
||
"""A small rounded label; a leading "v " is drawn as a check mark (the font has no such glyph)."""
|
||
mark = s.startswith("v ")
|
||
s = s[2:] if mark else s
|
||
f = font(size, weight)
|
||
tw = int(ImageDraw.Draw(Image.new("L", (1, 1))).textlength(s, font=f))
|
||
extra = int(size * 1.1) if mark else 0
|
||
w = tw + extra + int(size * 1.7)
|
||
h = int(size * 1.9)
|
||
layer = Image.new("RGBA", (w, h), (0, 0, 0, 0))
|
||
layer.alpha_composite(rr(w, h, h // 2, fill))
|
||
ImageDraw.Draw(layer).text((w / 2 + extra / 2, h / 2 - 1), s, font=f, fill=tuple(color) + (255,), anchor="mm")
|
||
if mark:
|
||
draw_check(layer, int(size * 1.15), h // 2, size * 0.8, 1.0, color, max(3, size // 7))
|
||
put_c(img, layer, cx, cy, scale, alpha)
|
||
|
||
|
||
# ---------------------------------------------------------------- the glossy "candy" letters of the title
|
||
|
||
CANDY = [((255, 99, 150), (222, 40, 110)), ((120, 210, 90), (60, 160, 60)), ((255, 170, 40), (240, 110, 20)), ((90, 170, 255), (50, 110, 230)),
|
||
((190, 120, 255), (130, 70, 220))]
|
||
|
||
|
||
@lru_cache(maxsize=64)
|
||
def candy_letter(ch: str, size: int, index: int) -> Image.Image:
|
||
f = font(size, "black")
|
||
box = f.getbbox(ch)
|
||
pad = 26
|
||
w, h = box[2] - box[0] + 2 * pad, box[3] - box[1] + 2 * pad
|
||
mask = Image.new("L", (w, h), 0)
|
||
ImageDraw.Draw(mask).text((pad - box[0], pad - box[1]), ch, font=f, fill=255)
|
||
top, bottom = CANDY[index % len(CANDY)]
|
||
body = Image.new("RGBA", (w, h))
|
||
px = body.load()
|
||
for y in range(h):
|
||
v = y / h
|
||
c = tuple(int(lerp(top[i], bottom[i], v)) for i in range(3))
|
||
for x in range(w):
|
||
px[x, y] = c + (255,)
|
||
# a lighter rim on the inside, a glossy highlight on the upper part
|
||
inner = mask.filter(ImageFilter.MinFilter(9)).filter(ImageFilter.GaussianBlur(3))
|
||
rim = ImageChops.subtract(mask, inner)
|
||
rim_img = Image.new("RGBA", (w, h), (255, 255, 255, 0))
|
||
rim_img.putalpha(rim.point(lambda v: int(v * 0.55)))
|
||
body.alpha_composite(rim_img)
|
||
shine = Image.new("L", (w, h), 0)
|
||
ImageDraw.Draw(shine).ellipse([w * 0.1, -h * 0.05, w * 0.9, h * 0.46], fill=150)
|
||
shine = ImageChops.multiply(shine.filter(ImageFilter.GaussianBlur(5)), inner)
|
||
gloss = Image.new("RGBA", (w, h), (255, 255, 255, 0))
|
||
gloss.putalpha(shine)
|
||
body.alpha_composite(gloss)
|
||
body.putalpha(mask)
|
||
out = Image.new("RGBA", (w + 10, h + 24), (0, 0, 0, 0))
|
||
shadow = Image.new("RGBA", out.size, (0, 0, 0, 0))
|
||
shadow.paste((40, 20, 60, 90), (5, 16), mask)
|
||
out.alpha_composite(shadow.filter(ImageFilter.GaussianBlur(6)))
|
||
out.alpha_composite(body, (5, 4))
|
||
return out
|
||
|
||
|
||
def candy_text(img, cx, cy, s, size, t, t0, step=0.08, alpha=1.0):
|
||
"""Letters pop in one after another, each with a little overshoot, then bob very slightly."""
|
||
f = font(size, "black")
|
||
advance = [f.getlength(ch) for ch in s]
|
||
total = sum(advance) + 4 * (len(s) - 1)
|
||
x = cx - total / 2
|
||
for i, ch in enumerate(s):
|
||
age = t - (t0 + i * step)
|
||
k = out_back(age / 0.35)
|
||
if ch != " " and age > 0:
|
||
letter = candy_letter(ch, size, i)
|
||
bob = math.sin((t - t0) * 4 + i * 0.8) * 4 * seg(age, 0.4, 0.8)
|
||
put_c(img, letter, x + advance[i] / 2, cy + bob + (1 - k) * 60, 0.6 + 0.4 * k, alpha * clamp(age / 0.12))
|
||
x += advance[i] + 4
|
||
|
||
|
||
# ---------------------------------------------------------------- scenes
|
||
# Times come from the strong beats: S[0] 1.14 s, S[1] 3.18 s ...
|
||
|
||
def scene_start(img, t):
|
||
"""The opening: a pill button "publish your frame"; the cursor comes, clicks on the strong beat, the button opens into the window."""
|
||
click = S[1]
|
||
if t >= click + 0.45:
|
||
return
|
||
ks = 0.9 if VERT else 1.0
|
||
intro = out_back(seg(t, 0.1, 0.7))
|
||
gone = seg(t, click + 0.1, click + 0.45)
|
||
press = 1 - 0.06 * clamp(1 - abs(t - click) / 0.1)
|
||
w, h = 760, 120
|
||
layer = Image.new("RGBA", (w + 120, h + 120), (0, 0, 0, 0))
|
||
layer.alpha_composite(card(w, h, h // 2, (255, 255, 255), blur=30, dy=12, alpha=44))
|
||
d = ImageDraw.Draw(layer)
|
||
cx0, cy0 = 60 + 70, 60 + h // 2
|
||
d.ellipse([cx0 - 38, cy0 - 38, cx0 + 38, cy0 + 38], fill=VIOLET + (255,))
|
||
d.line([(cx0, cy0 - 15), (cx0, cy0 + 15)], fill=(255, 255, 255, 255), width=6)
|
||
d.line([(cx0 - 15, cy0), (cx0 + 15, cy0)], fill=(255, 255, 255, 255), width=6)
|
||
d.text((60 + 150, 60 + h // 2 - 1), "Опубликовать свою рамку", font=font(40, "semi"), fill=INK + (255,), anchor="lm")
|
||
put_c(img, layer, CX, CY, lerp(0.9, 1.0, intro) * press * (1 + 0.25 * gone) * ks, clamp(intro * 1.3) * (1 - gone))
|
||
ripple(img, CX + 50 * ks, CY + 20, t, click, VIOLET, 120 * ks)
|
||
# the cursor comes from the corner to the button and stays on it
|
||
k = in_out(seg(t, 1.0, click - 0.1))
|
||
cx, cy = lerp(W - 120 + (0 if VERT else 0), CX + 50 * ks, k) - math.sin(k * math.pi) * 90, lerp(H + (-160 if VERT else 80), CY + 26, k)
|
||
put_c(img, cursor_sprite(), cx + 10, cy + 16, 1.0 - 0.08 * clamp(1 - abs(t - click) / 0.1), out_cubic(seg(t, 0.9, 1.3)))
|
||
|
||
|
||
def scene_upload(img, t): # noqa: C901
|
||
"""The key scene: the cursor drags a picture into the window, it uploads, a check is drawn, the window slides down, an avatar appears."""
|
||
t_in, t_grab, t_drop, t_done, t_down, t_avatar = S[1] + 0.25, S[1] + 1.25, S[2], S[3], S[3] + 0.55, S[3] + 0.9
|
||
leave = S[5] - 0.6
|
||
if t < t_in or t > leave + 0.8:
|
||
return
|
||
kv = 0.92 if VERT else 1.0 # the window is a little smaller on the narrow screen
|
||
wy0 = 900 if VERT else 590
|
||
# the window: grows out of the button, at the end it slides down
|
||
appear = out_back(seg(t, t_in, t_in + 0.6), 1.15)
|
||
down = in_cubic(seg(t, t_down, t_down + 0.75))
|
||
wx, wy = CX, wy0 + down * 900
|
||
walpha = (1 - seg(t, t_down + 0.35, t_down + 0.8))
|
||
if walpha > 0.01:
|
||
win = Image.new("RGBA", (1140, 840), (0, 0, 0, 0))
|
||
lx, ly = 570, 420
|
||
win.alpha_composite(card(1000, 700, 40), (lx - 500 - 68, ly - 350 - 68))
|
||
text(win, (lx - 440, ly - 290), "Новая рамка", 38, "bold", INK, "lm")
|
||
text(win, (lx + 440, ly - 290), "x", 40, "reg", GREY, "rm")
|
||
hot = out_cubic(seg(t, t_grab + 0.55, t_drop - 0.05))
|
||
zone_bg = tuple(int(lerp(c, z, hot)) for c, z in zip((250, 250, 252), (240, 234, 255)))
|
||
zone_line = tuple(int(lerp(c, z, hot)) for c, z in zip((196, 196, 206), VIOLET))
|
||
put_c(win, rr(860, 440, 30, zone_bg, zone_line, 3), lx, ly - 40, 1.0 + 0.012 * hot)
|
||
sunk = seg(t, t_drop, t_drop + 0.35)
|
||
if sunk < 0.05: # the empty drop zone
|
||
fade = 1 - sunk * 4
|
||
text(win, (lx, ly - 90), "Перетащите картинку сюда", 36, "semi", INK, "mm", alpha=fade)
|
||
text(win, (lx, ly - 36), "PNG или JPG, фото само станет кольцом", 26, "reg", GREY, "mm", alpha=fade)
|
||
prog = seg(t, t_drop + 0.35, t_done)
|
||
if t_drop + 0.2 <= t < t_done:
|
||
put_c(win, sunset(88), lx - 250, ly - 80, 1.0, out_cubic(seg(t, t_drop + 0.2, t_drop + 0.5)))
|
||
text(win, (lx - 180, ly - 100), "sunset-flower.png", 34, "semi", INK, "lm")
|
||
text(win, (lx - 180, ly - 58), "84 КБ", 26, "reg", GREY, "lm")
|
||
text(win, (lx + 290, ly - 100), "%d%%" % int(prog * 100), 34, "bold", VIOLET, "rm")
|
||
put_c(win, rr(580, 12, 6, (232, 232, 240)), lx, ly + 10)
|
||
put(win, rr(max(12, int(580 * prog)), 12, 6, VIOLET), lx - 290, ly + 4)
|
||
if t >= t_done:
|
||
k = out_back(seg(t, t_done, t_done + 0.45), 2.0)
|
||
put_c(win, disc(190, VIOLET), lx, ly - 100, k)
|
||
draw_check(win, lx, ly - 100, 90 * k, out_cubic(seg(t, t_done + 0.1, t_done + 0.55)), (255, 255, 255), 16)
|
||
text(win, (lx, ly + 40), "Готово", 40, "bold", INK, "mm", alpha=seg(t, t_done + 0.3, t_done + 0.6))
|
||
ripple(win, lx, ly - 100, t, t_done, VIOLET, 150)
|
||
put_c(win, rr(250, 76, 38, (240, 240, 244)), lx - 340, ly + 250)
|
||
text(win, (lx - 340, ly + 249), "Выбрать файл", 28, "semi", INK, "mm")
|
||
ready = seg(t, t_done, t_done + 0.4)
|
||
put_c(win, rr(280, 76, 38, tuple(int(lerp(c, v, ready)) for c, v in zip((226, 222, 240), VIOLET))), lx + 330, ly + 250)
|
||
text(win, (lx + 330, ly + 249), "Опубликовать", 28, "semi", (255, 255, 255), "mm")
|
||
put_c(img, win, wx, wy, lerp(0.45, 1.0, appear) * kv, clamp(appear * 1.6) * walpha)
|
||
if t >= t_done:
|
||
burst(img, CX, wy0 - 100 * kv + down * 900, t, t_done + 0.05, seed=3, n=46, power=560)
|
||
|
||
# the picture and the cursor: the cursor arrives, takes the picture, drags it into the zone
|
||
lift = clamp((t - t_grab) / 0.18) * (1 - seg(t, t_drop, t_drop + 0.15))
|
||
src = (CX - 250, wy0 + 560) if VERT else (330, 880)
|
||
zone_c = (CX, wy0 - 40 * kv)
|
||
start_cursor = (CX + 50 * (0.9 if VERT else 1.0), CY + 26)
|
||
if t < t_grab:
|
||
k = in_out(seg(t, t_in + 0.05, t_grab))
|
||
cur = (lerp(start_cursor[0], src[0] + 30, k) + math.sin(k * math.pi) * -120, lerp(start_cursor[1], src[1] + 40, k))
|
||
drag = None
|
||
else:
|
||
k = in_out(seg(t, t_grab + 0.2, t_drop))
|
||
cur = (lerp(src[0] + 30, zone_c[0] + 30, k), lerp(src[1] + 40, zone_c[1] + 30, k) - math.sin(k * math.pi) * 110)
|
||
drag = k
|
||
tile_pos = (src[0], src[1]) if t < t_grab else (cur[0] - 30, cur[1] - 40)
|
||
if t < t_drop + 0.4:
|
||
appear_pic = out_back(seg(t, t_in + 0.1, t_in + 0.6))
|
||
size = int(lerp(250, 300, lift))
|
||
pic = mask_round(sunset(size), 36)
|
||
shadow = soft_shadow(size, size, 36, blur=int(lerp(18, 50, lift) // 4 * 4), dy=int(lerp(10, 34, lift) // 4 * 4), alpha=int(lerp(40, 84, lift) // 6 * 6))
|
||
sink = seg(t, t_drop, t_drop + 0.35)
|
||
sc = appear_pic * (1 - 0.75 * out_cubic(sink)) * (1 + 0.04 * lift)
|
||
angle = 0.0 if drag is None else -8 * math.sin(clamp(drag) * math.pi) * (1 if drag < 1 else 0)
|
||
a = 1 - seg(t, t_drop + 0.25, t_drop + 0.45)
|
||
if t >= t_grab:
|
||
put_c(img, shadow, tile_pos[0], tile_pos[1], sc, a * 0.9, angle)
|
||
put_c(img, pic, tile_pos[0], tile_pos[1], sc, a, angle)
|
||
if t < t_drop + 0.6:
|
||
ripple(img, src[0] + 30, src[1] + 40, t, t_grab, (120, 80, 210), 60)
|
||
c_alpha = 1.0 * (1 - seg(t, t_drop + 0.25, t_drop + 0.55))
|
||
press = 1.0 - 0.08 * clamp(1 - abs(t - t_grab) / 0.1)
|
||
put_c(img, cursor_sprite(), cur[0] + 10, cur[1] + 16, press, c_alpha)
|
||
|
||
# the avatar with the new frame
|
||
if t >= t_avatar:
|
||
k = out_back(seg(t, t_avatar, t_avatar + 0.7), 1.9)
|
||
beat = 1 + 0.05 * clamp(1 - abs(t - S[4]) / 0.22)
|
||
move = in_out(seg(t, S[4] + 0.4, S[5] - 0.2))
|
||
if VERT: # a bigger avatar in the middle that then rises to the top while the tiles come in below
|
||
ax, ay = CX, lerp(wy0 - 20, 330, move)
|
||
sc = k * beat * lerp(1.18, 0.66, move)
|
||
else:
|
||
ax, ay = lerp(960, 420, move), lerp(560, 600, move)
|
||
sc = k * beat * lerp(1.0, 0.82, move)
|
||
out_alpha = 1.0
|
||
put_c(img, soft_shadow(300, 300, 150, blur=40, dy=24, alpha=50), ax, ay, sc, 0.9)
|
||
put_c(img, avatar(300), ax, ay, sc, out_alpha)
|
||
put_c(img, ring_of(390), ax, ay, sc * 1.0, out_alpha, angle=(1 - k) * -40)
|
||
burst(img, ax, ay, t, t_avatar + 0.05, seed=9, n=36, power=430, life=1.0, colors=[GOLD, (255, 140, 170), (140, 120, 255), (255, 255, 255)])
|
||
text(img, (ax, ay + 262 * sc), "Рамка принята", 34, "semi", INK, "mm", alpha=seg(t, t_avatar + 0.4, t_avatar + 0.8))
|
||
pill(img, ax, ay + 322 * sc, "v из вашей картинки", VIOLET, (255, 255, 255), 24, alpha=seg(t, t_avatar + 0.7, t_avatar + 1.1))
|
||
|
||
|
||
def scene_catalog(img, t):
|
||
"""A row of picture tiles glides across, the middle one is bigger; on a beat they fan out, the camera zooms into one and a pill says "added"."""
|
||
a0, a1 = S[4] + 0.5, S[9]
|
||
if t < a0 or t > a1 + 0.5:
|
||
return
|
||
size = 460 if VERT else 340
|
||
spacing = 520 if VERT else 410
|
||
center_x = CX if VERT else 1130
|
||
zoom_k = 0.9 if VERT else 1.1
|
||
reach = 560.0 if VERT else 700.0
|
||
fan_gap = 190 if VERT else 150
|
||
row_y = CY + (40 if VERT else 0)
|
||
appear = out_cubic(seg(t, a0, a0 + 1.4))
|
||
fan = in_out(seg(t, S[7], S[7] + 0.7)) * (1 - in_out(seg(t, S[8], S[8] + 0.45)))
|
||
zoom = in_out(seg(t, S[8], S[8] + 0.7)) * (1 - in_out(seg(t, S[9] - 0.5, S[9] + 0.3)))
|
||
morph = in_out(seg(t, S[7] + 0.2, S[7] + 0.65)) * (1 - in_out(seg(t, S[9] - 0.35, S[9])))
|
||
shift = (t - a0) * (190 if VERT else 150)
|
||
offset = center_x - 830 * spacing / 410
|
||
out_alpha = 1 - seg(t, S[9] - 0.2, S[9] + 0.4)
|
||
items = []
|
||
for i in range(-3, 12):
|
||
x_row = (i * spacing - shift) % (12 * spacing) - 2 * spacing + offset
|
||
x_row = lerp(W + 380 + i * 60, x_row, appear)
|
||
d = abs(x_row - center_x) / reach
|
||
scale = clamp(1.12 - 0.42 * d, 0.5, 1.12)
|
||
# the fan: all tiles gather to the middle and tilt
|
||
x_fan = center_x + (i % 5 - 2) * fan_gap
|
||
angle_fan = (i % 5 - 2) * 7
|
||
x = lerp(x_row, x_fan, fan)
|
||
angle = angle_fan * fan
|
||
scale = lerp(scale, 1.0 - abs(i % 5 - 2) * 0.05, fan)
|
||
items.append((scale * 100 - abs(i % 5 - 2) * fan * 10, i, x, angle, scale))
|
||
for _, i, x, angle, scale in sorted(items):
|
||
cy = row_y + math.sin(i * 1.3 + t * 1.2) * 10 * (1 - fan)
|
||
sprite = card(size, size, 36)
|
||
# each tile pops a little as it turns into the cat, one after another
|
||
local = clamp((t - (S[7] + 0.2 + (i % 5) * 0.04)) / 0.4)
|
||
pop = 1 + 0.14 * math.sin(math.pi * local) if 0 < local < 1 else 1.0
|
||
sc = scale * (1 + zoom_k * zoom) * pop
|
||
put_c(img, sprite, x, cy, sc, out_alpha, angle)
|
||
put_c(img, tile(i, size), x, cy, sc, out_alpha, angle)
|
||
if morph > 0.01:
|
||
put_c(img, tile(i, size, True), x, cy, sc, out_alpha * morph, angle)
|
||
burst(img, center_x, row_y, t, S[7] + 0.3, seed=21, n=34, power=470, life=0.9, colors=[(255, 150, 190), (255, 255, 255), (255, 210, 120), (190, 150, 255)])
|
||
if zoom > 0.6:
|
||
k = out_cubic(seg(zoom, 0.6, 1.0))
|
||
cxz, cyz = center_x, row_y
|
||
base = size * (1 + zoom_k * zoom)
|
||
flip = seg(t, S[8] + 0.9, S[8] + 1.1)
|
||
label = "v Добавлено" if flip >= 1 else "+ Добавить"
|
||
col = VIOLET if flip >= 1 else (24, 24, 28)
|
||
press = 1 + 0.12 * clamp(1 - abs(t - (S[8] + 1.0)) / 0.15)
|
||
pill(img, cxz - base / 2 + 160, cyz + base / 2 - 56, label, col, (255, 255, 255), 30, alpha=k * out_alpha, scale=press)
|
||
pill(img, cxz + base / 2 - 90, cyz - base / 2 + 56, "%d" % (1204 + (1 if flip >= 1 else 0)), (255, 255, 255), INK, 26, alpha=k * out_alpha)
|
||
if flip >= 1:
|
||
ripple(img, cxz - base / 2 + 160, cyz + base / 2 - 56, t, S[8] + 1.0, VIOLET, 90)
|
||
|
||
|
||
def scene_bubbles(img, t):
|
||
"""A chat panel: the picture behind the outgoing message changes on every beat while a stock grey bubble answers."""
|
||
b0, b1 = S[9], S[12]
|
||
if t < b0 or t > b1 + 0.6:
|
||
return
|
||
vy = 1.4 if VERT else 1.0 # the panel is taller on the tall screen
|
||
appear = out_back(seg(t, b0 + 0.1, b0 + 0.8), 1.3)
|
||
leave = in_cubic(seg(t, b1 - 0.1, b1 + 0.5))
|
||
cx, cy = CX, lerp(H + 220, CY + 20, appear) + leave * 900
|
||
Y = lambda v: cy + v * vy
|
||
put_c(img, card(1000, int(780 * vy), 48), cx, cy, 1.0, clamp(appear * 1.4) * (1 - leave))
|
||
text(img, (cx, Y(-330)), "Чат", 34, "semi", GREY, "mm", alpha=1 - leave)
|
||
# the stock bubble of TikTok
|
||
put_c(img, rr(560, 100, 38, (238, 238, 242)), cx - 160, Y(-200), 1.0, (1 - leave) * seg(t, b0 + 0.5, b0 + 0.9))
|
||
text(img, (cx - 160, Y(-200) - 1), "Привет! Как тебе мой пузырь?", 29, "reg", INK, "mm", alpha=(1 - leave) * seg(t, b0 + 0.5, b0 + 0.9))
|
||
# the custom bubble
|
||
idx = sum(1 for s in S if t >= s + 0.0 and s >= b0 and s <= b1 + 0.01)
|
||
since = t - (S[8 + max(0, idx)] if 8 + idx < len(S) else b0)
|
||
pop = out_back(clamp(since / 0.4), 2.0)
|
||
bw, bh = 640, 132
|
||
pic = mask_round(bubble_picture(idx, bw, bh), 46)
|
||
put_c(img, pic, cx + 150, Y(-40), lerp(0.92, 1.0, pop), (1 - leave) * seg(t, b0 + 0.9, b0 + 1.3))
|
||
text(img, (cx + 150, Y(-40) + 2), "Он виден и у меня, и у тебя", 30, "bold", (0, 0, 0), "mm", alpha=0.25 * (1 - leave) * seg(t, b0 + 0.9, b0 + 1.3))
|
||
text(img, (cx + 150, Y(-40) - 1), "Он виден и у меня, и у тебя", 30, "bold", (255, 255, 255), "mm", alpha=(1 - leave) * seg(t, b0 + 0.9, b0 + 1.3))
|
||
names = ["Конфетти", "Мята", "Закат", "Облака"]
|
||
pill(img, cx + 150, Y(80), "Пузырь «%s»" % names[idx % 4], (24, 24, 28), (255, 255, 255), 24, alpha=(1 - leave) * seg(t, b0 + 1.0, b0 + 1.4), scale=lerp(0.9, 1.0, pop))
|
||
# the other person's custom bubble answers
|
||
k = seg(t, S[10] + 0.1, S[10] + 0.6)
|
||
put_c(img, mask_round(bubble_picture(idx + 2, 520, 100), 40), cx - 190, Y(190), lerp(0.9, 1.0, out_back(k)), k * (1 - leave))
|
||
text(img, (cx - 190, Y(190) + 2), "У меня тоже есть мод", 28, "bold", (0, 0, 0), "mm", alpha=0.25 * k * (1 - leave))
|
||
text(img, (cx - 190, Y(190) - 1), "У меня тоже есть мод", 28, "bold", (255, 255, 255), "mm", alpha=k * (1 - leave))
|
||
burst(img, cx, Y(-40), t, S[11] + 0.02, seed=5, n=60, power=640, life=1.3)
|
||
|
||
|
||
def scene_publish(img, t):
|
||
"""Three short lines, one after another on the beats: big words that rise into place, with picture tiles drifting around."""
|
||
p0, p1 = S[12], S[14]
|
||
if t < p0 or t > p1 + 0.3:
|
||
return
|
||
lines = [["Публикуй", "рамки", "и пузыри"], ["Сначала", "проверка,", "потом каталог"], ["Жми", "«+»", "и она в TikTok"]] if VERT else \
|
||
[["Публикуй", "рамки", "и", "пузыри"], ["Сначала", "проверка,", "потом", "каталог"], ["Жми", "«+»", "и она", "в TikTok"]]
|
||
spots = [(210, 430, 230), (870, 1500, 260), (820, 380, 180), (240, 1560, 200)] if VERT else [(300, 330, 190), (1620, 770, 220), (1560, 250, 150), (360, 800, 160)]
|
||
size = 92 if VERT else 84
|
||
window = (p1 - p0) / 3
|
||
for n, words in enumerate(lines):
|
||
a, b = p0 + n * window, p0 + (n + 1) * window
|
||
if not a <= t < b + 0.1:
|
||
continue
|
||
out = seg(t, b - 0.25, b)
|
||
f = font(size, "semi")
|
||
draw = ImageDraw.Draw(Image.new("L", (1, 1)))
|
||
gap = 26
|
||
# on the tall screen the words stand in rows (a row per word group), on the wide one in a single row
|
||
rows = [words] if not VERT else [words[:1] if len(words) == 3 and n == 2 else words[:2] if n == 1 else words[:2], None]
|
||
rows = [[w for w in words[:1]], [w for w in words[1:]]] if VERT else [words]
|
||
for r, row in enumerate(rows):
|
||
widths = [draw.textlength(w, font=f) for w in row]
|
||
x = CX - (sum(widths) + gap * (len(row) - 1)) / 2
|
||
base_y = CY + ((r - 0.5) * 130 if VERT else 0)
|
||
for i, w in enumerate(row):
|
||
order = i + (0 if r == 0 else len(rows[0]))
|
||
k = out_back(seg(t, a + order * 0.11, a + order * 0.11 + 0.4), 1.6)
|
||
lift = (1 - k) * 46 - out * 40
|
||
accent = VIOLET if (n == 2 and w == "«+»") else INK
|
||
text(img, (x, base_y + lift), w, size, "semi", accent, "lm", alpha=clamp(k * 1.4) * (1 - out))
|
||
x += widths[i] + gap
|
||
for j, (tx, ty, sz) in enumerate(spots):
|
||
k = out_back(seg(t, a + 0.15 + j * 0.1, a + 0.7 + j * 0.1), 1.5)
|
||
bob = math.sin(t * 1.6 + j * 1.7) * 14
|
||
idx = n * 4 + j
|
||
put_c(img, card(sz, sz, int(sz * 0.12), blur=24, dy=10, alpha=40), tx, ty + bob, k, (1 - out))
|
||
put_c(img, tile(idx, sz), tx, ty + bob, k, (1 - out), angle=(j - 1.5) * 4 * (1 - k * 0.3))
|
||
|
||
|
||
def scene_title(img, t):
|
||
"""The title: LihonT in black, STORE in candy letters; then the logo."""
|
||
a = S[14] + 0.1
|
||
if t < a:
|
||
return
|
||
logo_t = S[15]
|
||
k1 = 1 - seg(t, logo_t - 0.1, logo_t + 0.3)
|
||
if VERT: # one above the other
|
||
text(img, (CX, CY - 230), "LihonT", 170, "semi", INK, "mm", alpha=out_cubic(seg(t, a, a + 0.4)) * k1)
|
||
candy_text(img, CX, CY - 40, "STORE", 190, t, a + 0.25, 0.09, alpha=k1)
|
||
text(img, (CX, CY + 160), "рамки и пузыри чата", 44, "reg", GREY, "mm", alpha=out_cubic(seg(t, a + 0.9, a + 1.3)) * k1)
|
||
text(img, (CX, CY + 220), "для вашего TikTok", 44, "reg", GREY, "mm", alpha=out_cubic(seg(t, a + 1.0, a + 1.4)) * k1)
|
||
logo_c, logo_size, caption_y = (CX, CY - 130), 560, CY + 300
|
||
else:
|
||
text(img, (700, 470), "LihonT", 150, "semi", INK, "mm", alpha=out_cubic(seg(t, a, a + 0.4)) * k1)
|
||
candy_text(img, 1230, 470, "STORE", 150, t, a + 0.25, 0.09, alpha=k1)
|
||
text(img, (960, 640), "рамки и пузыри чата для вашего TikTok", 40, "reg", GREY, "mm", alpha=out_cubic(seg(t, a + 0.9, a + 1.3)) * k1)
|
||
logo_c, logo_size, caption_y = (960, 480), 420, 780
|
||
if t >= logo_t - 0.05:
|
||
flower = Image.open(ROOT / "src" / "brand" / "logo_note_flower_gold_1024.png").convert("RGBA")
|
||
k = out_back(seg(t, logo_t, logo_t + 0.6), 1.5)
|
||
put_c(img, flower.resize((logo_size, logo_size), Image.Resampling.LANCZOS), logo_c[0], logo_c[1], k, clamp(k * 1.5))
|
||
text(img, (CX, caption_y), "lihont.space · t.me/lihont", 38, "semi", GREY, "mm", alpha=seg(t, logo_t + 0.5, logo_t + 0.9))
|
||
burst(img, logo_c[0], logo_c[1], t, logo_t + 0.1, seed=11, n=50, power=700, life=1.4)
|
||
|
||
|
||
# ---------------------------------------------------------------- the whole picture
|
||
|
||
def frame_at(t: float) -> Image.Image:
|
||
img = Image.new("RGBA", (W, H), (255, 255, 255, 255))
|
||
for scene in (scene_start, scene_upload, scene_catalog, scene_bubbles, scene_publish, scene_title):
|
||
scene(img, t)
|
||
fade_in = clamp(t / 0.25)
|
||
if fade_in < 1:
|
||
img.alpha_composite(Image.new("RGBA", (W, H), (255, 255, 255, int(255 * (1 - fade_in)))))
|
||
return img
|
||
|
||
|
||
def render_frame(index: int) -> bytes:
|
||
return frame_at(index / FPS).convert("RGB").tobytes()
|
||
|
||
|
||
def _init(song: str, vertical: bool) -> None:
|
||
configure(song)
|
||
set_format(vertical)
|
||
|
||
|
||
def sheet(path: Path, count: int = 24) -> None:
|
||
thumbs = []
|
||
for i in range(count):
|
||
t = DURATION * (i + 0.5) / count
|
||
thumbs.append(frame_at(t).convert("RGB").resize((480, 270), Image.Resampling.LANCZOS))
|
||
cols = 4
|
||
rows = (count + cols - 1) // cols
|
||
out = Image.new("RGB", (cols * 484, rows * 274), (200, 200, 205))
|
||
for i, th in enumerate(thumbs):
|
||
out.paste(th, ((i % cols) * 484 + 2, (i // cols) * 274 + 2))
|
||
out.save(path)
|
||
|
||
|
||
def main() -> None:
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("--song", default="store")
|
||
parser.add_argument("--still", type=float, nargs="*")
|
||
parser.add_argument("--sheet", action="store_true")
|
||
parser.add_argument("--vertical", action="store_true", help="9:16 (1080x1920) instead of 16:9")
|
||
parser.add_argument("--jobs", type=int, default=6)
|
||
args = parser.parse_args()
|
||
configure(args.song)
|
||
set_format(args.vertical)
|
||
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
||
if args.still:
|
||
for t in args.still:
|
||
path = OUT_DIR / ("still_%s%.2f.png" % ("v" if args.vertical else "", t))
|
||
frame_at(t).convert("RGB").save(path)
|
||
print(path)
|
||
return
|
||
if args.sheet:
|
||
path = OUT_DIR / "sheet.png"
|
||
sheet(path)
|
||
print(path)
|
||
return
|
||
out = OUT_DIR / ("store_openai_%s%s.mp4" % (args.song, "_9x16" if args.vertical else ""))
|
||
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(SONG), "-c:v", "libx264", "-preset", "medium", "-crf", "17", "-pix_fmt", "yuv420p",
|
||
"-af", "afade=t=out:st=%.2f:d=0.6" % (DURATION - 0.6), "-c:a", "aac", "-b:a", "192k", "-t", "%.2f" % DURATION, str(out)]
|
||
proc = subprocess.Popen(command, stdin=subprocess.PIPE)
|
||
with Pool(args.jobs, initializer=_init, initargs=(args.song, 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()
|