Files
LihonT/scripts/make_frame_cat.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

449 lines
23 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Profile frame "Ушки": a ring round the avatar with two ears, tufts of fur on the cheeks and a cowlick, sticker style
(black outline, white fur, pink inside the ears). The shape is drawn here from scratch.
A second frame, "Скафандр", is drawn the same way: a round helmet with a collar, side pieces, an antenna and rivets, and a glare
on the glass over the avatar.
usage (from the repository root):
py -X utf8 -B scripts/make_frame_cat.py # work/frames/cat.png, helmet.png (transparent) and the two previews
"""
from __future__ import annotations
import math
from pathlib import Path
from PIL import Image, ImageChops, ImageDraw, ImageFilter, ImageFont
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "work" / "frames"
SIZE = 1024
SS = 2 # supersampling
CX, CY = 512, 580 # centre of the avatar
HOLE = 290 # radius of the avatar window
RING = 332 # outer radius of the ring
LINE = 9 # outline width at SIZE
def pt(theta: float, r: float):
a = math.radians(theta)
return (CX + r * math.cos(a), CY - r * math.sin(a))
def curve(p0, p1, p2, n=18):
"""A quadratic curve p0 -> p2 bent towards p1."""
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 ear(side: int):
"""side -1 = left ear, +1 = right ear: the outer edge bulges, the inner edge is nearly straight."""
if side < 0:
base_out, base_in, tip = pt(134, 312), pt(100, 312), pt(116, 600)
else:
base_out, base_in, tip = pt(46, 312), pt(80, 312), pt(64, 600)
bend_out = (base_out[0] + side * 22, (base_out[1] + tip[1]) / 2 + 20)
bend_in = ((base_in[0] + tip[0]) / 2 - side * 2, (base_in[1] + tip[1]) / 2 + 6)
return curve(base_out, bend_out, tip) + curve(tip, bend_in, base_in)[1:]
def inner_ear(side: int):
"""The pink inside of the ear: the ear itself shrunk towards the middle of its base, so the white rim is even all round."""
outer = ear(side)
anchor = pt(118 if side < 0 else 62, 322)
k = 0.64
return [(anchor[0] + (x - anchor[0]) * k, anchor[1] + (y - anchor[1]) * k) for x, y in outer]
def tuft(theta: float, length: float, lean: float, width: float = 13):
"""A pointed lock of fur growing out of the ring at theta, leaning by `lean` degrees."""
a = pt(theta - width, RING - 25)
b = pt(theta + width, RING - 25)
tip = pt(theta + lean, RING + length)
return curve(a, ((a[0] + tip[0]) / 2 + 22, (a[1] + tip[1]) / 2 - 14), tip, 10) + curve(tip, ((b[0] + tip[0]) / 2 - 2, (b[1] + tip[1]) / 2 + 12), b, 10)[1:]
def shapes():
out = []
for side in (-1, 1):
out.append(ear(side))
# cheeks: three locks each side, the lower ones lean down
for theta, length, lean in ((162, 70, 10), (184, 78, 4), (206, 66, -4)):
out.append(tuft(theta, length, lean))
out.append(tuft(180 - theta, length, -lean))
# cowlick between the ears
out.append(curve(pt(98, RING - 30), pt(88, 470), pt(70, 402), 12) + curve(pt(70, 402), pt(86, 420), pt(80, RING - 28), 12)[1:])
return out
def build() -> Image.Image:
n = SIZE * SS
scale = lambda poly: [(x * SS, y * SS) for x, y in poly] # noqa: E731
mask = Image.new("L", (n, n), 0)
d = ImageDraw.Draw(mask)
d.ellipse([(CX - RING) * SS, (CY - RING) * SS, (CX + RING) * SS, (CY + RING) * SS], fill=255)
for poly in shapes():
d.polygon(scale(poly), fill=255)
k = LINE * SS * 2 + 1
outline = mask.filter(ImageFilter.MaxFilter(k if k % 2 else k + 1))
img = Image.new("RGBA", (n, n), (0, 0, 0, 0))
img.paste(Image.new("RGBA", (n, n), (22, 20, 26, 255)), (0, 0), outline)
img.paste(Image.new("RGBA", (n, n), (255, 252, 250, 255)), (0, 0), mask)
d = ImageDraw.Draw(img)
for side in (-1, 1): # pink inside the ears, with a thin outline
poly = scale(inner_ear(side))
d.polygon(poly, fill=(255, 176, 204, 255))
d.line(poly + [poly[0]], fill=(214, 110, 150, 255), width=LINE * SS // 3, joint="curve")
# the window of the avatar, with its own outline
hole = Image.new("L", (n, n), 0)
ImageDraw.Draw(hole).ellipse([(CX - HOLE) * SS, (CY - HOLE) * SS, (CX + HOLE) * SS, (CY + HOLE) * SS], fill=255)
ring_line = Image.new("L", (n, n), 0)
ImageDraw.Draw(ring_line).ellipse([(CX - HOLE - LINE) * SS, (CY - HOLE - LINE) * SS,
(CX + HOLE + LINE) * SS, (CY + HOLE + LINE) * SS], fill=255)
img.paste(Image.new("RGBA", (n, n), (22, 20, 26, 255)), (0, 0), ring_line)
clear = Image.new("RGBA", (n, n), (0, 0, 0, 0))
img.paste(clear, (0, 0), hole)
return img.resize((SIZE, SIZE), Image.LANCZOS)
def build_helmet() -> Image.Image:
n = SIZE * SS
ring = 352
sc = lambda v: v * SS # noqa: E731
mask = Image.new("L", (n, n), 0)
d = ImageDraw.Draw(mask)
d.ellipse([sc(CX - ring), sc(CY - ring), sc(CX + ring), sc(CY + ring)], fill=255)
for side in (-1, 1): # side pieces of the helmet
x = CX + side * (ring - 6)
d.rounded_rectangle([sc(x - 34), sc(CY - 74), sc(x + 34), sc(CY + 74)], radius=sc(30), fill=255)
d.rounded_rectangle([sc(CX - 290), sc(CY + ring - 40), sc(CX + 290), sc(CY + ring + 74)], radius=sc(44), fill=255) # collar
ax, ay = pt(52, ring - 10) # antenna
tx, ty = pt(52, ring + 120)
d.line([sc(ax), sc(ay), sc(tx), sc(ty)], fill=255, width=sc(14))
d.ellipse([sc(tx - 24), sc(ty - 24), sc(tx + 24), sc(ty + 24)], fill=255)
k = LINE * SS * 2 + 1
outline = mask.filter(ImageFilter.MaxFilter(k if k % 2 else k + 1))
ink, metal = (22, 20, 26, 255), (226, 233, 242, 255)
img = Image.new("RGBA", (n, n), (0, 0, 0, 0))
img.paste(Image.new("RGBA", (n, n), ink), (0, 0), outline)
img.paste(Image.new("RGBA", (n, n), metal), (0, 0), mask)
d = ImageDraw.Draw(img)
# shading on the lower right of the ring, so it reads as a rounded metal
shade = Image.new("L", (n, n), 0)
sd = ImageDraw.Draw(shade)
sd.ellipse([sc(CX - ring + 26), sc(CY - ring + 26), sc(CX + ring + 40), sc(CY + ring + 40)], fill=255)
sd.ellipse([sc(CX - ring - 10), sc(CY - ring - 10), sc(CX + ring - 26), sc(CY + ring - 26)], fill=0)
img.paste(Image.new("RGBA", (n, n), (160, 176, 200, 255)), (0, 0), Image.composite(shade, Image.new("L", (n, n), 0), mask))
# orange stripe on the collar, grooves, side pieces
d.rounded_rectangle([sc(CX - 250), sc(CY + ring + 8), sc(CX + 250), sc(CY + ring + 34)], radius=sc(12), fill=(255, 150, 60, 255), outline=ink, width=sc(4))
for gx in range(-210, 211, 70):
d.line([sc(CX + gx), sc(CY + ring - 24), sc(CX + gx), sc(CY + ring + 2)], fill=(150, 165, 190, 255), width=sc(6))
for side in (-1, 1):
x = CX + side * (ring - 6)
d.rounded_rectangle([sc(x - 16), sc(CY - 40), sc(x + 16), sc(CY + 40)], radius=sc(12), fill=(255, 150, 60, 255), outline=ink, width=sc(4))
d.ellipse([sc(tx - 14), sc(ty - 14), sc(tx + 14), sc(ty + 14)], fill=(255, 90, 90, 255))
# rivets round the ring
for theta in range(0, 360, 20):
if 218 <= theta <= 322:
continue # hidden behind the collar
x, y = pt(theta, (HOLE + ring) / 2 + 4)
d.ellipse([sc(x - 8), sc(y - 8), sc(x + 8), sc(y + 8)], fill=(150, 165, 190, 255), outline=ink, width=sc(3))
# the window: dark rim round the glass
hole = Image.new("L", (n, n), 0)
ImageDraw.Draw(hole).ellipse([sc(CX - HOLE), sc(CY - HOLE), sc(CX + HOLE), sc(CY + HOLE)], fill=255)
rim = Image.new("L", (n, n), 0)
ImageDraw.Draw(rim).ellipse([sc(CX - HOLE - 22), sc(CY - HOLE - 22), sc(CX + HOLE + 22), sc(CY + HOLE + 22)], fill=255)
img.paste(Image.new("RGBA", (n, n), (48, 56, 84, 255)), (0, 0), rim)
inner = Image.new("L", (n, n), 0)
ImageDraw.Draw(inner).ellipse([sc(CX - HOLE - 12), sc(CY - HOLE - 12), sc(CX + HOLE + 12), sc(CY + HOLE + 12)], fill=255)
img.paste(Image.new("RGBA", (n, n), (48, 56, 84, 255)), (0, 0), inner)
img.paste(Image.new("RGBA", (n, n), (0, 0, 0, 0)), (0, 0), hole)
# glare on the glass: it lies over the avatar, so it is drawn last and kept light
glare = Image.new("RGBA", (n, n), (0, 0, 0, 0))
gd = ImageDraw.Draw(glare)
gd.arc([sc(CX - HOLE + 40), sc(CY - HOLE + 40), sc(CX + HOLE - 40), sc(CY + HOLE - 40)], start=200, end=262, fill=(255, 255, 255, 150), width=sc(26))
gd.arc([sc(CX - HOLE + 40), sc(CY - HOLE + 40), sc(CX + HOLE - 40), sc(CY + HOLE - 40)], start=272, end=290, fill=(255, 255, 255, 150), width=sc(26))
img.alpha_composite(glare)
return img.resize((SIZE, SIZE), Image.LANCZOS)
INK = (22, 20, 26, 255)
def ellipse_poly(cx, cy, rx, ry, angle_deg, n=36):
c, s_ = math.cos(math.radians(angle_deg)), math.sin(math.radians(angle_deg))
out = []
for i in range(n):
t = 2 * math.pi * i / n
x, y = rx * math.cos(t), ry * math.sin(t)
out.append((cx + x * c - y * s_, cy + x * s_ + y * c))
return out
def lock(theta, r0, length, width, lean=0.0):
"""A pointed tongue growing outwards from the ring at theta (flame, leaf): a bulging belly and a tip."""
a, b = pt(theta - width, r0), pt(theta + width, r0)
tip = pt(theta + lean, r0 + length)
mid_l, mid_r = pt(theta - width * 1.5, r0 + length * 0.45), pt(theta + width * 1.2, r0 + length * 0.5)
return curve(a, mid_l, tip, 14) + curve(tip, mid_r, b, 14)[1:]
def make_frame(ring, base, shapes_fn, paint_fn, rim=INK, rim_width=0):
"""Common recipe: shapes -> outline -> base colour -> details -> window cut with its own rim."""
n = SIZE * SS
sc = lambda poly: [(x * SS, y * SS) for x, y in poly] # noqa: E731
mask = Image.new("L", (n, n), 0)
d = ImageDraw.Draw(mask)
d.ellipse([(CX - ring) * SS, (CY - ring) * SS, (CX + ring) * SS, (CY + ring) * SS], fill=255)
for poly in shapes_fn():
d.polygon(sc(poly), fill=255)
k = LINE * SS * 2 + 1
outline = mask.filter(ImageFilter.MaxFilter(k if k % 2 else k + 1))
img = Image.new("RGBA", (n, n), (0, 0, 0, 0))
img.paste(Image.new("RGBA", (n, n), INK), (0, 0), outline)
img.paste(Image.new("RGBA", (n, n), base), (0, 0), mask)
d = ImageDraw.Draw(img)
paint_fn(d, sc)
pad = LINE + rim_width
ring_line = Image.new("L", (n, n), 0)
ImageDraw.Draw(ring_line).ellipse([(CX - HOLE - pad) * SS, (CY - HOLE - pad) * SS, (CX + HOLE + pad) * SS, (CY + HOLE + pad) * SS], fill=255)
img.paste(Image.new("RGBA", (n, n), INK), (0, 0), ring_line)
if rim_width:
inner = Image.new("L", (n, n), 0)
ImageDraw.Draw(inner).ellipse([(CX - HOLE - rim_width) * SS, (CY - HOLE - rim_width) * SS, (CX + HOLE + rim_width) * SS, (CY + HOLE + rim_width) * SS], fill=255)
img.paste(Image.new("RGBA", (n, n), rim), (0, 0), inner)
hole = Image.new("L", (n, n), 0)
ImageDraw.Draw(hole).ellipse([(CX - HOLE) * SS, (CY - HOLE) * SS, (CX + HOLE) * SS, (CY + HOLE) * SS], fill=255)
img.paste(Image.new("RGBA", (n, n), (0, 0, 0, 0)), (0, 0), hole)
return img.resize((SIZE, SIZE), Image.LANCZOS)
# --- the three richer frames: every element is shaded (gradient, light on the upper left, shadow on the lower right) ---
N = SIZE * SS
def wob(poly, amount=3.0, seed=0.0):
"""A hand-drawn feel: the outline is pushed in and out a little by a slow wave."""
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
def mask_of(*polys, circles=()):
m = Image.new("L", (N, 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 union(masks):
out = masks[0]
for m in masks[1:]:
out = ImageChops.lighter(out, m)
return out
def gradient(top, bottom):
g = Image.linear_gradient("L").resize((N, N))
return Image.merge("RGBA", [g.point(lambda v, a=top[i], b=bottom[i]: a + (b - a) * v // 255) for i in range(3)]
+ [Image.new("L", (N, N), 255)])
def shade(img, mask, top, bottom, line=None, light=0.6, dark=0.22, reach=7, line_w=3):
"""Paint one element: gradient fill, light edge on the upper left, shadow edge on the lower right, a thin darker line."""
img.paste(gradient(top, bottom), (0, 0), mask)
dx = reach * SS
lit = ImageChops.subtract(mask, ImageChops.offset(mask, dx, dx)).filter(ImageFilter.GaussianBlur(SS))
shd = ImageChops.subtract(mask, ImageChops.offset(mask, -dx, -dx)).filter(ImageFilter.GaussianBlur(SS))
img.paste(Image.new("RGBA", (N, N), (255, 255, 255, 255)), (0, 0), lit.point(lambda v: int(v * light)))
img.paste(Image.new("RGBA", (N, N), (60, 20, 40, 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)
img.paste(Image.new("RGBA", (N, N), line), (0, 0), edge.point(lambda v: min(255, v * 2)))
def finish_frame(img, silhouette, ring_color=INK):
"""Black outline round the whole silhouette (under everything), then the window cut with a thin rim."""
k = LINE * SS * 2 + 1
out = silhouette.filter(ImageFilter.MaxFilter(k))
base = Image.new("RGBA", (N, N), (0, 0, 0, 0))
base.paste(Image.new("RGBA", (N, N), INK), (0, 0), out)
base.alpha_composite(img)
rim = Image.new("L", (N, N), 0)
ImageDraw.Draw(rim).ellipse([(CX - HOLE - LINE) * SS, (CY - HOLE - LINE) * SS,
(CX + HOLE + LINE) * SS, (CY + HOLE + LINE) * SS], fill=255)
base.paste(Image.new("RGBA", (N, N), ring_color), (0, 0), rim)
hole = Image.new("L", (N, N), 0)
ImageDraw.Draw(hole).ellipse([(CX - HOLE) * SS, (CY - HOLE) * SS, (CX + HOLE) * SS, (CY + HOLE) * SS], fill=255)
base.paste(Image.new("RGBA", (N, N), (0, 0, 0, 0)), (0, 0), hole)
return base.resize((SIZE, SIZE), Image.LANCZOS)
def ring_mask(r):
m = Image.new("L", (N, N), 0)
ImageDraw.Draw(m).ellipse([(CX - r) * SS, (CY - r) * SS, (CX + r) * SS, (CY + r) * SS], fill=255)
return m
def star(x, y, r, spin=0.0):
pts = []
for i in range(8):
a = math.radians(i * 45 + spin)
rr = r if i % 2 == 0 else r * 0.28
pts.append((x + rr * math.cos(a), y + rr * math.sin(a)))
return pts
def build_flower() -> Image.Image:
img = Image.new("RGBA", (N, N), (0, 0, 0, 0))
back, front = [], []
for i in range(12):
th = i * 30
back.append(wob(ellipse_poly(*pt(th, 372), 92, 64, -th, 40), 2.6, i))
front.append(wob(ellipse_poly(*pt(th + 15, 352), 74, 52, -(th + 15), 40), 2.2, i + 5))
silhouette = union([mask_of(*back), mask_of(*front), ring_mask(340)])
for poly in back:
shade(img, mask_of(poly), (255, 168, 204), (226, 92, 148), line=(176, 58, 112, 255))
for poly in front:
shade(img, mask_of(poly), (255, 226, 238), (255, 168, 204), line=(214, 96, 150, 255))
d = ImageDraw.Draw(img)
for i in range(12): # veins
th = i * 30 + 15
for off in (-7, 0, 7):
a, b = pt(th + off * 0.5, 332), pt(th + off * 0.18, 392)
d.line([a[0] * SS, a[1] * SS, b[0] * SS, b[1] * SS], fill=(226, 120, 168, 200), width=3 * SS)
shade(img, ring_mask(340), (255, 246, 214), (255, 196, 96), line=(186, 120, 30, 255), reach=9)
shade(img, ring_mask(312), (255, 228, 120), (240, 160, 40), line=(186, 120, 30, 255), reach=6, light=0.5)
for th in range(0, 360, 15): # pearls on the golden ring
x, y = pt(th, 326)
shade(img, mask_of(circles=[(x, y, 7)]), (255, 255, 255), (220, 214, 230), line=(150, 120, 140, 255), reach=3, line_w=2)
return finish_frame(img, silhouette)
def build_crown() -> Image.Image:
img = Image.new("RGBA", (N, N), (0, 0, 0, 0))
body = wob([(322, 296), (308, 130), (354, 164), (418, 222), (466, 150), (512, 84), (558, 150), (606, 222), (670, 164),
(716, 130), (702, 296)], 2.2, 1)
band = wob([(CX - 196, 268), (CX + 196, 268), (CX + 204, 322), (CX - 204, 322)], 1.6, 3)
gems = [(308, 124, 26, (255, 130, 150), (204, 34, 72)), (512, 80, 30, (140, 214, 255), (36, 112, 214)),
(716, 124, 26, (255, 130, 150), (204, 34, 72))]
silhouette = union([mask_of(body), mask_of(band), ring_mask(340), mask_of(circles=[(x, y, r + 4) for x, y, r, _, _ in gems])])
shade(img, ring_mask(340), (255, 238, 150), (236, 160, 24), line=(150, 92, 10, 255), reach=10)
shade(img, ring_mask(312), (255, 214, 90), (226, 138, 20), line=(150, 92, 10, 255), reach=6, light=0.4)
shade(img, mask_of(body), (255, 240, 150), (240, 172, 30), line=(150, 92, 10, 255), reach=9)
inner = wob([(344, 286), (336, 178), (420, 244), (512, 120), (604, 244), (688, 178), (680, 286)], 1.8, 7)
shade(img, mask_of(inner), (255, 250, 200), (255, 206, 80), line=None, reach=6, light=0.35)
shade(img, mask_of(band), (255, 226, 110), (214, 128, 16), line=(150, 92, 10, 255), reach=7)
for gx in (-132, -66, 0, 66, 132): # pearls on the band
shade(img, mask_of(circles=[(CX + gx, 295, 15)]), (255, 255, 255), (214, 210, 232), line=(120, 100, 130, 255), reach=4, line_w=2)
for (x, y, r, a, b) in gems:
shade(img, mask_of(circles=[(x, y, r)]), a, b, line=(110, 20, 50, 255), reach=7, light=0.7)
d = ImageDraw.Draw(img)
d.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 th in range(0, 360, 20): # rivets
if 56 <= th <= 124:
continue
x, y = pt(th, 326)
shade(img, mask_of(circles=[(x, y, 8)]), (255, 232, 140), (190, 120, 16), line=(120, 70, 10, 255), reach=3, line_w=2)
d = ImageDraw.Draw(img)
for (x, y, r, spin) in ((250, 190, 30, 0), (790, 220, 24, 20), (610, 60, 20, 10)): # sparkles
poly = [(px * SS, py * SS) for px, py in star(x, y, r, spin)]
d.polygon(poly, fill=(255, 252, 220, 255), outline=(150, 92, 10, 255))
return finish_frame(img, silhouette)
def build_flame() -> Image.Image:
img = Image.new("RGBA", (N, N), (0, 0, 0, 0))
rnd = [0.9, 1.15, 0.8, 1.2, 1.0, 1.25, 0.85, 1.1, 0.95, 1.2, 0.8, 1.05, 0.9, 1.1, 0.85, 1.2, 0.95, 1.0, 0.8, 1.15, 1.0]
spec = []
for i, theta in enumerate(range(-30, 211, 12)):
up = max(0.0, math.sin(math.radians(theta)))
length = (96 + 120 * up) * rnd[i % len(rnd)]
spec.append((theta, length, 9 + 3 * up, (-1) ** i * (5 + 4 * up)))
outer = [wob(lock(t, 300, ln, w, lean), 2.4, t) for t, ln, w, lean in spec]
mid = [wob(lock(t, 306, ln * 0.7, w * 0.72, lean * 0.7), 1.8, t + 3) for t, ln, w, lean in spec]
core = [wob(lock(t, 312, ln * 0.4, w * 0.45, lean * 0.4), 1.4, t + 7) for t, ln, w, lean in spec]
sparks = []
for (x, y, sz, tilt) in ((196, 150, 17, 30), (842, 190, 15, -35), (120, 330, 13, 20), (900, 430, 14, -20), (360, 70, 13, 10)):
sparks.append(wob(ellipse_poly(x, y, sz * 1.6, sz * 0.8, tilt, 24), 0.8, x))
silhouette = union([mask_of(*outer), mask_of(*sparks), ring_mask(340)])
for poly in outer:
shade(img, mask_of(poly), (255, 150, 60), (214, 44, 28), line=(130, 24, 16, 255), reach=7, light=0.4)
for poly in mid:
shade(img, mask_of(poly), (255, 214, 90), (255, 130, 32), line=(214, 84, 16, 255), reach=5, light=0.4, line_w=2)
for poly in core:
shade(img, mask_of(poly), (255, 252, 214), (255, 214, 96), line=None, reach=4, light=0.3)
for poly in sparks:
shade(img, mask_of(poly), (255, 246, 190), (255, 168, 40), line=(214, 84, 16, 255), reach=3, light=0.3, line_w=2)
shade(img, ring_mask(340), (255, 160, 70), (206, 40, 28), line=(130, 24, 16, 255), reach=9)
shade(img, ring_mask(314), (255, 214, 96), (255, 124, 30), line=(214, 84, 16, 255), reach=6, light=0.5)
return finish_frame(img, silhouette)
def preview(frame: Image.Image, title: str, subtitle: str) -> Image.Image:
w, h = 1080, 1350
bg = Image.new("RGBA", (w, h), (255, 236, 196, 255))
d = ImageDraw.Draw(bg)
for gx in range(0, w, 60):
for gy in range(0, h, 60):
d.ellipse([gx, gy, gx + 5, gy + 5], fill=(240, 214, 160, 255))
avatar = Image.new("RGBA", (2 * HOLE, 2 * HOLE), (0, 0, 0, 0))
ad = ImageDraw.Draw(avatar)
for i in range(2 * HOLE): # a soft gradient stands in for a photo
t = i / (2 * HOLE)
ad.line([(0, i), (2 * HOLE, i)], fill=(int(120 + 100 * t), int(150 - 30 * t), int(230 - 80 * t), 255))
ad.ellipse([HOLE - 90, HOLE - 150, HOLE + 90, HOLE + 30], fill=(255, 230, 210, 255))
ad.ellipse([HOLE - 190, HOLE + 70, HOLE + 190, HOLE + 520], fill=(255, 230, 210, 255))
m = Image.new("L", avatar.size, 0)
ImageDraw.Draw(m).ellipse([0, 0, 2 * HOLE - 1, 2 * HOLE - 1], fill=255)
layer = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
layer.paste(avatar, (CX - HOLE, CY - HOLE), m)
layer.alpha_composite(frame)
bg.alpha_composite(layer.resize((900, 900), Image.LANCZOS), (90, 90))
font = None
for name in ("C:/Windows/Fonts/arialbd.ttf", "C:/Windows/Fonts/segoeuib.ttf"):
try:
font = ImageFont.truetype(name, 54)
break
except OSError:
pass
d = ImageDraw.Draw(bg)
d.text((w / 2, 1090), title, font=font, fill=(40, 30, 40, 255), anchor="mm")
d.text((w / 2, 1170), subtitle, font=font, fill=(150, 90, 110, 255), anchor="mm")
return bg
def main() -> None:
OUT.mkdir(parents=True, exist_ok=True)
frame = build()
frame.save(OUT / "cat.png")
preview(frame, "Рамка «Ушки»", "уши, шерсть на щеках и хохолок").convert("RGB").save(OUT / "cat_preview.png")
helmet = build_helmet()
helmet.save(OUT / "helmet.png")
preview(helmet, "Рамка «Скафандр»", "шлем, антенна, блик на стекле").convert("RGB").save(OUT / "helmet_preview.png")
for name, title, sub, fn in (("flower", "Рамка «Цветок»", "двенадцать лепестков, как наш логотип", build_flower),
("crown", "Рамка «Корона»", "золотой обод, три камня", build_crown),
("flame", "Рамка «Пламя»", "под огоньки стрика", build_flame)):
frame_img = fn()
frame_img.save(OUT / (name + ".png"))
preview(frame_img, title, sub).convert("RGB").save(OUT / (name + "_preview.png"))
print(OUT / "cat.png")
if __name__ == "__main__":
main()