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>
335 lines
16 KiB
Python
335 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
The Серийчик animals of LihonT: a kitten, a puppy and a lion cub that grow with the days of a streak (cub, teen, adult, elder) and go grey
|
|
and sad when the streak is about to burn out. They are drawn here from circles and curves (Pillow only, supersampled), so they are ours.
|
|
|
|
py -X utf8 -B scripts/make_pet_designs.py # work/pets/preview.png and work/pets/<animal>_<stage>[_grey].png
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OUT = ROOT / "work" / "pets"
|
|
S = 3 # supersampling
|
|
SIZE = 512 # final side of one picture
|
|
W = SIZE * S
|
|
INK = (58, 40, 52)
|
|
WHITE = (255, 255, 255)
|
|
|
|
PALETTES = {
|
|
"cat": {"fur": (255, 170, 82), "dark": (226, 120, 40), "belly": (255, 232, 200), "ear": (255, 150, 160)},
|
|
"dog": {"fur": (232, 188, 132), "dark": (150, 98, 60), "belly": (255, 240, 214), "ear": (150, 98, 60)},
|
|
"lion": {"fur": (255, 206, 96), "dark": (222, 140, 40), "belly": (255, 236, 190), "ear": (255, 170, 120)},
|
|
}
|
|
STAGES = ["cub", "teen", "adult", "elder"]
|
|
STAGE_NAMES = {"cub": "малыш", "teen": "подросток", "adult": "взрослый", "elder": "старший"}
|
|
# head size, body size, eye size, body height relative to the head (all relative to the picture)
|
|
LOOK = {"cub": (0.34, 0.20, 1.25, 0.18), "teen": (0.29, 0.25, 1.05, 0.30), "adult": (0.25, 0.30, 0.9, 0.38), "elder": (0.25, 0.30, 0.9, 0.38)}
|
|
|
|
|
|
FRAMES = 24 # one loop: 24 frames at 12 per second
|
|
|
|
|
|
def rot(point, origin, degrees):
|
|
a = math.radians(degrees)
|
|
dx, dy = point[0] - origin[0], point[1] - origin[1]
|
|
return (origin[0] + dx * math.cos(a) - dy * math.sin(a), origin[1] + dx * math.sin(a) + dy * math.cos(a))
|
|
|
|
|
|
def motion(frame):
|
|
"""(tail angle, left ear angle, right ear angle, blink 0..1, bob) of one frame of the loop."""
|
|
tail = 13 * math.sin(math.tau * frame / 12) # the tail wags twice in a loop
|
|
left = -16 * math.sin(math.pi * (frame - 5) / 4) if 5 <= frame <= 9 else 0.0 # the left ear twitches
|
|
right = 16 * math.sin(math.pi * (frame - 15) / 4) if 15 <= frame <= 19 else 0.0 # the right ear, a little later
|
|
blink = 1.0 if frame in (12, 13) else 0.0
|
|
bob = 0.004 * math.sin(math.tau * frame / 24)
|
|
return tail, left, right, blink, bob
|
|
|
|
|
|
def grey(color):
|
|
g = int(0.3 * color[0] + 0.59 * color[1] + 0.11 * color[2])
|
|
return (int(g * 0.9 + 60 * 0.1), int(g * 0.9 + 60 * 0.1), int(g * 0.95 + 70 * 0.1))
|
|
|
|
|
|
class Canvas:
|
|
def __init__(self, sad):
|
|
self.img = Image.new("RGBA", (W, W), (0, 0, 0, 0))
|
|
self.d = ImageDraw.Draw(self.img)
|
|
self.sad = sad
|
|
|
|
K = 0.74
|
|
|
|
def t(self, x, y):
|
|
return (0.46 + (x - 0.5) * self.K) * W, (0.54 + (y - 0.5) * self.K) * W
|
|
|
|
def c(self, color):
|
|
return grey(color) if self.sad else color
|
|
|
|
def ell(self, cx, cy, rx, ry, fill, outline=True, width=0.012):
|
|
(cx, cy), rx, ry = self.t(cx, cy), rx * W * self.K, ry * W * self.K
|
|
if outline:
|
|
o = width * W
|
|
self.d.ellipse([cx - rx - o, cy - ry - o, cx + rx + o, cy + ry + o], fill=INK)
|
|
self.d.ellipse([cx - rx, cy - ry, cx + rx, cy + ry], fill=self.c(fill))
|
|
|
|
def poly(self, pts, fill, outline=True, width=0.012):
|
|
pts = [self.t(x, y) for x, y in pts]
|
|
if outline:
|
|
self.d.line(pts + [pts[0]], fill=INK, width=int(width * 2 * W), joint="curve")
|
|
self.d.polygon(pts, fill=self.c(fill))
|
|
|
|
def line(self, pts, width, color=INK):
|
|
self.d.line([self.t(x, y) for x, y in pts], fill=color, width=int(width * W * self.K), joint="curve")
|
|
|
|
def arc(self, cx, cy, rx, ry, a0, a1, width, color=INK):
|
|
(x0, y0), (x1, y1) = self.t(cx - rx, cy - ry), self.t(cx + rx, cy + ry)
|
|
self.d.arc([x0, y0, x1, y1], a0, a1, fill=color, width=int(width * W * self.K))
|
|
|
|
|
|
def tube(cv, p0, c1, c2, p3, w0, w1, fill, tip=None, tuft=None):
|
|
"""A tail: a cubic curve that gets thinner towards its end, with round ends and an outline. `tip` colours the last fifth; `tuft` puts a
|
|
leaf-shaped brush of that colour at the end, pointing the way the tail points."""
|
|
pts = []
|
|
for i in range(61):
|
|
t = i / 60
|
|
u = 1 - t
|
|
pts.append((u ** 3 * p0[0] + 3 * u * u * t * c1[0] + 3 * u * t * t * c2[0] + t ** 3 * p3[0],
|
|
u ** 3 * p0[1] + 3 * u * u * t * c1[1] + 3 * u * t * t * c2[1] + t ** 3 * p3[1]))
|
|
for outline in (True, False):
|
|
for i, (x, y) in enumerate(pts):
|
|
t = i / 60
|
|
w = w0 + (w1 - w0) * t + (0.026 if outline else 0)
|
|
(cx, cy) = cv.t(x, y)
|
|
r = w * W * cv.K / 2
|
|
color = INK if outline else cv.c(tip if tip is not None and t > 0.8 else fill)
|
|
cv.d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=color)
|
|
if tuft is not None: # a brush of three round tufts
|
|
x1, y1 = pts[-1]
|
|
for dx, dy, r in ((0.0, 0.0, 0.042), (-0.034, 0.03, 0.034), (0.034, 0.03, 0.034)):
|
|
cv.ell(x1 + dx, y1 + dy, r, r, tuft)
|
|
|
|
|
|
def scarf(cv, cx, head_cy, head, body_cy):
|
|
"""A scarf round the neck with one end hanging down; drawn before the head so the chin lies over it."""
|
|
red, dark = (232, 72, 92), (186, 44, 70)
|
|
cv.ell(cx, head_cy + head * 0.86, head * 0.82, head * 0.2, red)
|
|
x = cx + head * 0.42
|
|
top = head_cy + head * 0.95
|
|
cv.poly([(x - head * 0.17, top), (x + head * 0.17, top), (x + head * 0.2, top + head * 0.82), (x - head * 0.2, top + head * 0.82)], red)
|
|
cv.poly([(x - head * 0.185, top + head * 0.5), (x + head * 0.19, top + head * 0.5), (x + head * 0.2, top + head * 0.62),
|
|
(x - head * 0.2, top + head * 0.62)], dark, outline=False)
|
|
|
|
|
|
def eyes(cv, cx, cy, spread, size, sad, blink=0.0):
|
|
def box(x0, y0, x1, y1, fill):
|
|
(a, b), (c, d) = cv.t(x0, y0), cv.t(x1, y1)
|
|
cv.d.ellipse([a, b, c, d], fill=fill)
|
|
|
|
for sign in (-1, 1):
|
|
x = cx + sign * spread
|
|
if blink > 0.5: # a closed eye is a small arc
|
|
cv.arc(x, cy + size * 0.2, size * 0.95, size * 0.7, 200, 340, 0.011)
|
|
continue
|
|
box(x - size, cy - size * 1.15, x + size, cy + size * 1.15, INK)
|
|
box(x - size * 0.78, cy - size * 0.95, x + size * 0.78, cy + size * 0.95, (70, 42, 40) if not sad else (80, 80, 90))
|
|
box(x - size * 0.35, cy - size * 0.7, x + size * 0.12, cy - size * 0.2, WHITE)
|
|
box(x + size * 0.08, cy + size * 0.1, x + size * 0.38, cy + size * 0.4, WHITE)
|
|
if sad: # a drooping brow
|
|
cv.line([(x - size * 1.1, cy - size * (1.55 + 0.3 * sign)), (x + size * 1.1, cy - size * (1.55 - 0.3 * sign))], 0.012)
|
|
|
|
|
|
def face(cv, animal, cx, cy, head, sad, stage, blink=0.0):
|
|
pal = PALETTES[animal]
|
|
size = LOOK[stage][2] * 0.032
|
|
eyes(cv, cx, cy - head * 0.02, head * 0.42, size, sad, blink)
|
|
cv.ell(cx - head * 0.62, cy + head * 0.22, head * 0.16, head * 0.1, (255, 150, 150), outline=False)
|
|
cv.ell(cx + head * 0.62, cy + head * 0.22, head * 0.16, head * 0.1, (255, 150, 150), outline=False)
|
|
nose = (cx, cy + head * 0.2)
|
|
if animal == "dog":
|
|
cv.ell(nose[0], nose[1], head * 0.14, head * 0.1, (70, 50, 52), outline=False)
|
|
else:
|
|
cv.poly([(nose[0] - head * 0.1, nose[1] - head * 0.05), (nose[0] + head * 0.1, nose[1] - head * 0.05), (nose[0], nose[1] + head * 0.06)],
|
|
(255, 128, 140), outline=False)
|
|
my = nose[1] + head * 0.1
|
|
if sad:
|
|
cv.arc(cx - head * 0.1, my + head * 0.12, head * 0.1, head * 0.08, 200, 340, 0.011)
|
|
cv.arc(cx + head * 0.1, my + head * 0.12, head * 0.1, head * 0.08, 200, 340, 0.011)
|
|
else:
|
|
cv.arc(cx - head * 0.1, my, head * 0.1, head * 0.1, 10, 170, 0.011)
|
|
cv.arc(cx + head * 0.1, my, head * 0.1, head * 0.1, 10, 170, 0.011)
|
|
if animal == "dog" and stage != "elder":
|
|
cv.ell(cx, my + head * 0.17, head * 0.09, head * 0.1, (255, 120, 135), width=0.008)
|
|
if animal == "cat":
|
|
for sign in (-1, 1):
|
|
for k, dy in enumerate((-0.02, 0.06)):
|
|
cv.line([(cx + sign * head * 0.38, cy + head * (0.28 + dy)), (cx + sign * head * 0.95, cy + head * (0.2 + dy * 2.2 + 0.08 * k))], 0.005, (90, 70, 80))
|
|
|
|
|
|
def animal(kind, stage, sad=False, frame=0):
|
|
tail_a, ear_l, ear_r, blink, bob = motion(frame) if frame is not None else (0, 0, 0, 0.0, 0.0)
|
|
if sad: # a sad animal sways little
|
|
tail_a, ear_l, ear_r = tail_a * 0.3, ear_l * 0.5, ear_r * 0.5
|
|
cv = Canvas(sad)
|
|
pal = PALETTES[kind]
|
|
head, body, _, height = LOOK[stage]
|
|
cx = 0.5
|
|
bottom = 0.93
|
|
body_h = height + 0.10
|
|
body_cy = bottom - body_h * 0.5 - 0.02
|
|
head_cy = body_cy - body_h * 0.5 - head * 0.45 + (0.1 if stage == "cub" else 0.0)
|
|
if stage == "cub":
|
|
head_cy = bottom - 0.02 - body_h * 0.55 - head * 0.55
|
|
head_cy -= bob
|
|
# tail (it swings about its root)
|
|
fx = cx + body
|
|
|
|
def swing(points):
|
|
root = points[0]
|
|
return [rot(q, root, tail_a) for q in points]
|
|
|
|
if kind == "cat": # out to the side, up, and curling back at the end like a question mark
|
|
q = swing([(fx - 0.02, body_cy + body_h * 0.3), (fx + 0.36, body_cy + body_h * 0.55), (fx + 0.42, body_cy - body_h * 0.75),
|
|
(fx + 0.12, body_cy - body_h * 0.72)])
|
|
tube(cv, q[0], q[1], q[2], q[3], 0.075, 0.045, pal["fur"], tip=pal["dark"])
|
|
elif kind == "dog": # a short curl
|
|
q = swing([(fx - 0.02, body_cy + body_h * 0.1), (fx + 0.2, body_cy + body_h * 0.2), (fx + 0.22, body_cy - body_h * 0.4),
|
|
(fx + 0.06, body_cy - body_h * 0.4)])
|
|
tube(cv, q[0], q[1], q[2], q[3], 0.07, 0.05, pal["fur"])
|
|
else: # a lion's tail swings out and down with a brush at the end
|
|
q = swing([(fx - 0.02, body_cy + body_h * 0.3), (fx + 0.25, body_cy + body_h * 0.05), (fx + 0.36, body_cy + body_h * 0.6),
|
|
(fx + 0.2, body_cy + body_h * 0.62)])
|
|
tube(cv, q[0], q[1], q[2], q[3], 0.05, 0.035, pal["fur"], tuft=pal["dark"])
|
|
# body, belly, legs
|
|
cv.ell(cx, body_cy, body, body_h * 0.5, pal["fur"])
|
|
cv.ell(cx, body_cy + body_h * 0.08, body * 0.62, body_h * 0.34, pal["belly"], outline=False)
|
|
for sign in (-1, 1):
|
|
cv.ell(cx + sign * body * 0.52, bottom - 0.035, 0.07, 0.045, pal["fur"])
|
|
cv.ell(cx + sign * body * 0.52, bottom - 0.02, 0.045, 0.02, pal["belly"], outline=False)
|
|
# arms
|
|
for sign in (-1, 1):
|
|
cv.ell(cx + sign * (body + 0.012), body_cy - body_h * 0.05, 0.035, 0.07, pal["fur"])
|
|
if stage == "elder" and kind != "lion":
|
|
scarf(cv, cx, head_cy, head, body_cy)
|
|
# mane of the lion behind the head
|
|
if kind == "lion" and stage != "cub":
|
|
rings = {"teen": 0.6, "adult": 1.0, "elder": 1.15}[stage]
|
|
for k in range(14):
|
|
a = k * math.tau / 14
|
|
r = head * (1.0 + 0.1 * rings)
|
|
cv.ell(cx + math.cos(a) * r, head_cy + math.sin(a) * r * 0.95, head * 0.3 * rings + head * 0.1, head * 0.3 * rings + head * 0.1, pal["dark"])
|
|
# ears
|
|
if kind == "cat":
|
|
for sign in (-1, 1):
|
|
base = (cx + sign * head * 0.55, head_cy - head * 0.5)
|
|
a = ear_l if sign < 0 else ear_r
|
|
outer = [rot(q, base, a) for q in [(cx + sign * head * 0.28, head_cy - head * 0.7), (cx + sign * head * 0.98, head_cy - head * 1.12),
|
|
(cx + sign * head * 0.92, head_cy - head * 0.2)]]
|
|
inner = [rot(q, base, a) for q in [(cx + sign * head * 0.42, head_cy - head * 0.68), (cx + sign * head * 0.84, head_cy - head * 0.92),
|
|
(cx + sign * head * 0.8, head_cy - head * 0.38)]]
|
|
cv.poly(outer, pal["fur"])
|
|
cv.poly(inner, pal["ear"], outline=False)
|
|
elif kind == "dog":
|
|
for sign in (-1, 1):
|
|
root = (cx + sign * head * 0.8, head_cy - head * 0.4)
|
|
a = ear_l if sign < 0 else ear_r
|
|
ex, ey = rot((cx + sign * head * 0.92, head_cy - head * 0.1 + (head * 0.15 if stage != "cub" else head * 0.28)), root, a)
|
|
cv.ell(ex, ey, head * 0.3, head * 0.55, pal["ear"])
|
|
else:
|
|
for sign in (-1, 1):
|
|
root = (cx + sign * head * 0.55, head_cy - head * 0.6)
|
|
a = ear_l if sign < 0 else ear_r
|
|
ex, ey = rot((cx + sign * head * 0.72, head_cy - head * 0.78), root, a * 1.5)
|
|
cv.ell(ex, ey, head * 0.2, head * 0.2, pal["fur"])
|
|
cv.ell(ex, ey, head * 0.1, head * 0.1, pal["ear"], outline=False)
|
|
# head
|
|
cv.ell(cx, head_cy, head * 1.05, head * 0.92, pal["fur"])
|
|
if kind == "cat": # stripes on the forehead
|
|
for dx in (-0.16, 0, 0.16):
|
|
cv.line([(cx + head * dx, head_cy - head * 0.88), (cx + head * dx * 1.1, head_cy - head * 0.5)], 0.012, cv.c(pal["dark"]))
|
|
cv.ell(cx, head_cy + head * 0.32, head * 0.55, head * 0.4, pal["belly"], outline=False)
|
|
face(cv, kind, cx, head_cy + head * 0.02, head, sad, stage, blink)
|
|
# what an elder wears (the crown of a lion; the scarf of the others was put on before the head)
|
|
if stage == "elder" and kind == "lion":
|
|
cv.poly([(cx - head * 0.55, head_cy - head * 0.95), (cx - head * 0.4, head_cy - head * 1.45), (cx - head * 0.15, head_cy - head * 1.15),
|
|
(cx, head_cy - head * 1.55), (cx + head * 0.15, head_cy - head * 1.15), (cx + head * 0.4, head_cy - head * 1.45),
|
|
(cx + head * 0.55, head_cy - head * 0.95)], (255, 214, 64))
|
|
return cv.img.resize((SIZE, SIZE), Image.LANCZOS)
|
|
|
|
|
|
def sheet(images, path):
|
|
cell = SIZE // 2
|
|
pad = 18
|
|
cols = len(STAGES)
|
|
rows = len(images) // cols
|
|
out = Image.new("RGB", (cols * (cell + pad) + pad, rows * (cell + pad + 28) + pad), (252, 248, 240))
|
|
d = ImageDraw.Draw(out)
|
|
try:
|
|
font = ImageFont.truetype("segoeuib.ttf", 22)
|
|
except OSError:
|
|
font = ImageFont.load_default()
|
|
for i, (name, img) in enumerate(images):
|
|
r, c = divmod(i, cols)
|
|
x, y = pad + c * (cell + pad), pad + r * (cell + pad + 28)
|
|
out.paste(img.resize((cell, cell), Image.LANCZOS), (x, y), img.resize((cell, cell), Image.LANCZOS))
|
|
d.text((x + cell // 2, y + cell + 8), name, fill=(70, 60, 70), font=font, anchor="mt")
|
|
out.save(path)
|
|
|
|
|
|
def animated(path):
|
|
"""One GIF with every animal of every stage moving at once."""
|
|
cell = 200
|
|
pad = 12
|
|
cols, rows = len(STAGES), 3
|
|
frames = []
|
|
ground = (252, 248, 240)
|
|
for f in range(FRAMES):
|
|
sheet_img = Image.new("RGB", (cols * (cell + pad) + pad, rows * (cell + pad) + pad), ground)
|
|
for r, kind in enumerate(("cat", "dog", "lion")):
|
|
for c, stage in enumerate(STAGES):
|
|
img = animal(kind, stage, frame=f).resize((cell, cell), Image.LANCZOS)
|
|
sheet_img.paste(img, (pad + c * (cell + pad), pad + r * (cell + pad)), img)
|
|
frames.append(sheet_img.quantize(colors=96, method=Image.MEDIANCUT, dither=Image.Dither.NONE))
|
|
frames[0].save(path, save_all=True, append_images=frames[1:], duration=83, loop=0, disposal=1, optimize=False)
|
|
|
|
|
|
def strips(folder):
|
|
"""The sheets the mod downloads: for each animal, stage and state one strip of 8 frames (every third frame of the loop), 160 px each."""
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
cell = 160
|
|
for kind in ("cat", "dog", "lion"):
|
|
for stage in STAGES:
|
|
for sad, state in ((False, "lit"), (True, "grey")):
|
|
sheet_img = Image.new("RGBA", (cell * 8, cell), (0, 0, 0, 0))
|
|
for i in range(8):
|
|
sheet_img.paste(animal(kind, stage, sad=sad, frame=i * 3).resize((cell, cell), Image.LANCZOS), (i * cell, 0))
|
|
sheet_img.save(folder / ("%s_%s_%s.png" % (kind, stage, state)), optimize=True)
|
|
|
|
|
|
def main():
|
|
OUT.mkdir(parents=True, exist_ok=True)
|
|
images = []
|
|
for kind in ("cat", "dog", "lion"):
|
|
for stage in STAGES:
|
|
img = animal(kind, stage)
|
|
img.save(OUT / ("%s_%s.png" % (kind, stage)))
|
|
animal(kind, stage, sad=True).save(OUT / ("%s_%s_grey.png" % (kind, stage)))
|
|
images.append(({"cat": "кот", "dog": "пёс", "lion": "лев"}[kind] + ": " + STAGE_NAMES[stage], img))
|
|
sheet(images, OUT / "preview.png")
|
|
animated(OUT / "preview_anim.gif")
|
|
sad = [("грустный " + n, animal(k, "teen", True)) for n, k in (("кот", "cat"), ("пёс", "dog"), ("лев", "lion"))]
|
|
sad.append(("", Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))))
|
|
sheet(sad, OUT / "preview_grey.png")
|
|
strips(ROOT / "src" / "web" / "assets" / "pets" / "strips")
|
|
print(OUT / "preview.png")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|