Files
LihonT/scripts/frame_sprites.py
cheb0chik d40c17b083 LihonT 2 Serena beta
Avatar frames that follow the outline of the avatar (ring and parts hung on it), packs of frames that arrive from a catalog on the
site by themselves, the frame shown to other members through the members service, save an avatar by user name, the diary of the
mod, the seek bar on every video, the beta version line and its what is new, plus tests, the catalog builder, the sprites and the
scripts that draw them.

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

247 lines
11 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The sprites of the frames of the pack "basic": small transparent pictures (an ear, a lock of fur, a crown, a petal, a flame...)
that the application hangs on the outline of an avatar of any shape. Each sprite is drawn here from scratch, shaded, with a dark
outline; its "up" is the direction pointing away from the avatar.
usage (from the repository root):
py -X utf8 -B scripts/frame_sprites.py # frames/basic/*.png
"""
from __future__ import annotations
import math
from pathlib import Path
from PIL import Image, ImageChops, ImageDraw, ImageFilter
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "frames" / "basic"
SS = 4
INK = (22, 20, 26, 255)
LINE = 5 # outline width in sprite pixels
class Sprite:
def __init__(self, w: int, h: int):
self.w, self.h = w, h
self.n = (w * SS, h * SS)
self.img = Image.new("RGBA", self.n, (0, 0, 0, 0))
self.masks = []
def mask(self, *polys, circles=()):
m = Image.new("L", self.n, 0)
d = ImageDraw.Draw(m)
for poly in polys:
d.polygon([(x * SS, y * SS) for x, y in poly], fill=255)
for (x, y, r) in circles:
d.ellipse([(x - r) * SS, (y - r) * SS, (x + r) * SS, (y + r) * SS], fill=255)
return m
def gradient(self, top, bottom):
g = Image.linear_gradient("L").resize(self.n)
chans = [g.point(lambda v, a=top[i], b=bottom[i]: a + (b - a) * v // 255) for i in range(3)]
return Image.merge("RGBA", chans + [Image.new("L", self.n, 255)])
def shade(self, mask, top, bottom, line=None, light=0.55, dark=0.2, reach=4, line_w=2):
self.masks.append(mask)
self.img.paste(self.gradient(top, bottom), (0, 0), mask)
dx = reach * SS
lit = ImageChops.subtract(mask, ImageChops.offset(mask, dx, dx)).filter(ImageFilter.GaussianBlur(SS * 0.6))
shd = ImageChops.subtract(mask, ImageChops.offset(mask, -dx, -dx)).filter(ImageFilter.GaussianBlur(SS * 0.6))
self.img.paste(Image.new("RGBA", self.n, (255, 255, 255, 255)), (0, 0), lit.point(lambda v: int(v * light)))
self.img.paste(Image.new("RGBA", self.n, (60, 30, 50, 255)), (0, 0), shd.point(lambda v: min(255, int(v * dark * 2))))
if line:
k = line_w * SS * 2 + 1
edge = ImageChops.subtract(mask.filter(ImageFilter.MaxFilter(k)), mask)
self.img.paste(Image.new("RGBA", self.n, line), (0, 0), edge.point(lambda v: min(255, v * 2)))
def draw(self):
return ImageDraw.Draw(self.img)
def save(self, name):
"""Adds the dark outline round everything drawn, then saves at the sprite's own size."""
union = self.masks[0]
for m in self.masks[1:]:
union = ImageChops.lighter(union, m)
k = LINE * SS * 2 + 1
out = union.filter(ImageFilter.MaxFilter(k))
base = Image.new("RGBA", self.n, (0, 0, 0, 0))
base.paste(Image.new("RGBA", self.n, INK), (0, 0), out)
base.alpha_composite(self.img)
OUT.mkdir(parents=True, exist_ok=True)
base.resize((self.w, self.h), Image.LANCZOS).save(OUT / name, optimize=True)
def curve(p0, p1, p2, n=16):
return [((1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t * t * p2[0],
(1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t * t * p2[1]) for t in (i / n for i in range(n + 1))]
def ellipse(cx, cy, rx, ry, angle=0.0, n=40):
c, s = math.cos(math.radians(angle)), math.sin(math.radians(angle))
return [(cx + rx * math.cos(2 * math.pi * i / n) * c - ry * math.sin(2 * math.pi * i / n) * s,
cy + rx * math.cos(2 * math.pi * i / n) * s + ry * math.sin(2 * math.pi * i / n) * c) for i in range(n)]
def wob(poly, amount=1.2, seed=0.0):
cx = sum(x for x, _ in poly) / len(poly)
cy = sum(y for _, y in poly) / len(poly)
out = []
for i, (x, y) in enumerate(poly):
k = math.sin(i * 0.9 + seed) * amount + math.sin(i * 0.37 + seed * 2) * amount * 0.6
dx, dy = x - cx, y - cy
ln = math.hypot(dx, dy) or 1.0
out.append((x + dx / ln * k, y + dy / ln * k))
return out
# ---------------------------------------------------------------- cat
def cat_ear():
s = Sprite(170, 160)
body = curve((16, 152), (4, 56), (86, 6), 18) + curve((86, 6), (134, 56), (156, 152), 18)[1:]
s.shade(s.mask(wob(body, 0.8, 1)), (255, 253, 250), (232, 226, 236), line=(120, 110, 130, 255), reach=5)
inner = [(86 + (x - 86) * 0.58, 146 + (y - 146) * 0.6) for x, y in body]
s.shade(s.mask(inner), (255, 190, 214), (240, 130, 172), line=(214, 96, 140, 255), light=0.35, reach=3)
s.save("cat-ear.png")
def cat_tuft():
s = Sprite(150, 130)
body = curve((10, 124), (6, 46), (112, 6), 18) + curve((112, 6), (92, 70), (132, 124), 18)[1:]
s.shade(s.mask(wob(body, 0.8, 3)), (255, 253, 250), (232, 226, 236), line=(120, 110, 130, 255), reach=4)
s.save("cat-tuft.png")
def cat_cowlick():
s = Sprite(110, 130)
body = curve((30, 124), (22, 40), (82, 10), 14) + curve((82, 10), (70, 46), (50, 34), 8)[1:] + curve((50, 34), (60, 80), (74, 124), 10)[1:]
s.shade(s.mask(wob(body, 0.6, 5)), (255, 253, 250), (232, 226, 236), line=(120, 110, 130, 255), reach=4)
s.save("cat-cowlick.png")
# ---------------------------------------------------------------- helmet
def helmet_antenna():
s = Sprite(80, 170)
s.shade(s.mask([(34, 160), (46, 160), (44, 50), (36, 50)]), (236, 240, 248), (168, 182, 206), line=(110, 120, 150, 255), reach=3)
s.shade(s.mask(circles=[(40, 36, 26)]), (255, 150, 150), (214, 50, 60), line=(130, 20, 36, 255), reach=5, light=0.7)
s.draw().ellipse([(28 - 5) * SS, (26 - 5) * SS, (28 + 4) * SS, (26 + 4) * SS], fill=(255, 255, 255, 220))
s.save("helmet-antenna.png")
def helmet_pod():
s = Sprite(110, 150)
s.shade(s.mask(), (0, 0, 0), (0, 0, 0)) # placeholder so masks is never empty
s.img = Image.new("RGBA", s.n, (0, 0, 0, 0))
s.masks = []
body = s.mask(circles=[])
d = ImageDraw.Draw(body)
d.rounded_rectangle([14 * SS, 12 * SS, 96 * SS, 140 * SS], radius=30 * SS, fill=255)
s.shade(body, (236, 240, 248), (168, 182, 206), line=(110, 120, 150, 255), reach=5)
orange = s.mask()
ImageDraw.Draw(orange).rounded_rectangle([36 * SS, 38 * SS, 74 * SS, 112 * SS], radius=14 * SS, fill=255)
s.shade(orange, (255, 190, 100), (240, 120, 30), line=(150, 70, 10, 255), reach=3)
s.save("helmet-pod.png")
def helmet_collar():
s = Sprite(300, 100)
s.img = Image.new("RGBA", s.n, (0, 0, 0, 0))
body = Image.new("L", s.n, 0)
ImageDraw.Draw(body).rounded_rectangle([10 * SS, 8 * SS, 290 * SS, 92 * SS], radius=36 * SS, fill=255)
s.shade(body, (236, 240, 248), (168, 182, 206), line=(110, 120, 150, 255), reach=6)
stripe = Image.new("L", s.n, 0)
ImageDraw.Draw(stripe).rounded_rectangle([34 * SS, 52 * SS, 266 * SS, 74 * SS], radius=11 * SS, fill=255)
s.shade(stripe, (255, 190, 100), (240, 120, 30), line=(150, 70, 10, 255), reach=3)
d = s.draw()
for gx in range(60, 250, 46):
d.line([gx * SS, 18 * SS, gx * SS, 40 * SS], fill=(150, 165, 190, 255), width=5 * SS)
s.save("helmet-collar.png")
def helmet_glare():
s = Sprite(220, 220)
g = Image.new("L", s.n, 0)
d = ImageDraw.Draw(g)
d.arc([20 * SS, 20 * SS, 200 * SS, 200 * SS], start=200, end=262, fill=255, width=24 * SS)
d.arc([20 * SS, 20 * SS, 200 * SS, 200 * SS], start=272, end=292, fill=255, width=24 * SS)
s.img.paste(Image.new("RGBA", s.n, (255, 255, 255, 150)), (0, 0), g)
s.img.resize((s.w, s.h), Image.LANCZOS).save(OUT / "helmet-glare.png", optimize=True)
# ---------------------------------------------------------------- crown
def crown():
s = Sprite(340, 230)
body = [(30, 214), (14, 70), (62, 104), (118, 150), (170, 70), (170, 62), (222, 150), (278, 104), (326, 70), (310, 214)]
body = [(30, 214), (20, 70), (70, 112), (118, 150), (170, 40), (222, 150), (270, 112), (320, 70), (310, 214)]
s.shade(s.mask(wob(body, 0.8, 1)), (255, 240, 150), (240, 172, 30), line=(150, 92, 10, 255), reach=6)
inner = [(52, 206), (44, 100), (96, 142), (170, 70), (244, 142), (296, 100), (288, 206)]
s.shade(s.mask(wob(inner, 0.8, 4)), (255, 250, 200), (255, 206, 80), reach=4, light=0.35)
band = s.mask()
ImageDraw.Draw(band).rounded_rectangle([22 * SS, 178 * SS, 318 * SS, 222 * SS], radius=16 * SS, fill=255)
s.shade(band, (255, 226, 110), (214, 128, 16), line=(150, 92, 10, 255), reach=5)
for (x, y, r, a, b) in ((20, 66, 20, (255, 130, 150), (204, 34, 72)), (170, 34, 24, (140, 214, 255), (36, 112, 214)),
(320, 66, 20, (255, 130, 150), (204, 34, 72))):
s.shade(s.mask(circles=[(x, y, r)]), a, b, line=(110, 20, 50, 255), reach=5, light=0.7)
s.draw().ellipse([(x - r * 0.5) * SS, (y - r * 0.55) * SS, (x - r * 0.12) * SS, (y - r * 0.2) * SS], fill=(255, 255, 255, 230))
for gx in (80, 125, 170, 215, 260):
s.shade(s.mask(circles=[(gx, 200, 9)]), (255, 255, 255), (214, 210, 232), line=(120, 100, 130, 255), reach=3, line_w=1)
s.save("crown.png")
# ---------------------------------------------------------------- flower
def petal(name, top, bottom, line, heart_top, heart_bottom):
s = Sprite(150, 190)
body = ellipse(75, 100, 62, 84, 0, 44)
s.shade(s.mask(wob(body, 1.2, 2)), top, bottom, line=line, reach=5)
inner = ellipse(75, 108, 36, 54, 0, 36)
s.shade(s.mask(wob(inner, 0.8, 5)), heart_top, heart_bottom, line=None, reach=4, light=0.3)
d = s.draw()
for off in (-14, 0, 14):
d.line([(75 + off * 0.4) * SS, 150 * SS, (75 + off * 0.1) * SS, 66 * SS], fill=line[:3] + (170,), width=3 * SS)
s.save(name)
# ---------------------------------------------------------------- flame
def flame(name, h, w, lean):
s = Sprite(w, h)
cx = w / 2
def tongue(scale, base_y, k):
width = (w / 2 - 8) * scale
height = (h - 16) * k
a, b = (cx - width, base_y), (cx + width, base_y)
tip = (cx + lean * scale, base_y - height)
return curve(a, (cx - width * 1.35, base_y - height * 0.45), tip, 16) + curve(tip, (cx + width * 1.15, base_y - height * 0.5), b, 16)[1:]
s.shade(s.mask(wob(tongue(1.0, h - 4, 1.0), 1.3, 1)), (255, 150, 60), (214, 44, 28), line=(130, 24, 16, 255), reach=5, light=0.4)
s.shade(s.mask(wob(tongue(0.68, h - 6, 0.7), 1.0, 2)), (255, 214, 90), (255, 130, 32), line=(214, 84, 16, 255), reach=4, light=0.4)
s.shade(s.mask(wob(tongue(0.36, h - 8, 0.4), 0.8, 3)), (255, 252, 214), (255, 214, 96), reach=3, light=0.3)
s.save(name)
def main() -> None:
cat_ear()
cat_tuft()
cat_cowlick()
helmet_antenna()
helmet_pod()
helmet_collar()
helmet_glare()
crown()
petal("petal-back.png", (255, 168, 204), (226, 92, 148), (176, 58, 112, 255), (255, 214, 232), (255, 168, 204))
petal("petal-front.png", (255, 230, 240), (255, 176, 210), (214, 96, 150, 255), (255, 244, 248), (255, 206, 226))
flame("flame-tall.png", 210, 120, 8)
flame("flame-mid.png", 160, 110, -6)
flame("flame-short.png", 110, 96, 4)
for p in sorted(OUT.glob("*.png")):
print(p.name, p.stat().st_size)
if __name__ == "__main__":
main()