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

129 lines
5.3 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 -*-
"""
Shows a frame on an avatar of each shape the mod knows (the outlines are those of Shapes.java), as it is drawn in the application:
the avatar keeps the window of the frame, the shape lies inside the circle of that window.
usage (from the repository root):
py -X utf8 -B scripts/make_frame_on_shape.py [frame] # work/frames/<frame>_on_flower.png and <frame>_on_shapes.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]
FRAMES = ROOT / "work" / "frames"
CX, CY, HOLE = 512, 580, 290
SS = 4
def shape_polys(name, s):
"""Outlines as polygons in a box of side s (the shape sits in the middle square), like Shapes.path."""
pts = []
if name == "flower":
return [("circle", 0.5 * s, 0.5 * s, 0.31 * s)] + [
("circle", s * (0.5 + 0.31 * math.cos(p * math.pi / 3 - math.pi / 2)),
s * (0.5 + 0.31 * math.sin(p * math.pi / 3 - math.pi / 2)), 0.19 * s) for p in range(6)]
if name == "squircle":
return [("round", 0, 0, s, 0.34 * s)]
if name == "hexagon":
pts = [(s * (0.5 + 0.5 * math.cos(math.pi / 6 + c * math.pi / 3 - math.pi / 2 + math.pi / 6)),
s * (0.5 + 0.5 * math.sin(math.pi / 6 + c * math.pi / 3 - math.pi / 2 + math.pi / 6))) for c in range(6)]
elif name == "heart":
def bez(p0, p1, p2, p3, n=24):
return [((1 - t) ** 3 * p0[0] + 3 * (1 - t) ** 2 * t * p1[0] + 3 * (1 - t) * t * t * p2[0] + t ** 3 * p3[0],
(1 - t) ** 3 * p0[1] + 3 * (1 - t) ** 2 * t * p1[1] + 3 * (1 - t) * t * t * p2[1] + t ** 3 * p3[1])
for t in (i / n for i in range(n + 1))]
k = lambda a, b: (a * s, b * s) # noqa: E731
pts = (bez(k(.5, .94), k(.10, .62), k(.02, .40), k(.10, .24)) + bez(k(.10, .24), k(.18, .08), k(.40, .06), k(.5, .27))
+ bez(k(.5, .27), k(.60, .06), k(.82, .08), k(.90, .24)) + bez(k(.90, .24), k(.98, .40), k(.90, .62), k(.5, .94)))
elif name == "triangle":
pts = [(0.5 * s, 0.05 * s), (0.96 * s, 0.86 * s), (0.04 * s, 0.86 * s)]
else:
return [("circle", 0.5 * s, 0.5 * s, 0.5 * s)]
return [("poly", pts)]
def shape_mask(name, s):
m = Image.new("L", (s * SS, s * SS), 0)
d = ImageDraw.Draw(m)
for item in shape_polys(name, s * SS):
if item[0] == "circle":
_, x, y, r = item
d.ellipse([x - r, y - r, x + r, y + r], fill=255)
elif item[0] == "round":
_, x, y, side, rad = item
d.rounded_rectangle([x, y, x + side, y + side], radius=rad, fill=255)
else:
d.polygon(item[1], fill=255)
return m.resize((s, s), Image.LANCZOS)
def avatar(s):
"""A stand-in picture: a gradient with a head and shoulders."""
img = Image.new("RGBA", (s, s))
d = ImageDraw.Draw(img)
for i in range(s):
t = i / s
d.line([(0, i), (s, i)], fill=(int(120 + 100 * t), int(150 - 30 * t), int(230 - 80 * t), 255))
d.ellipse([s * 0.34, s * 0.2, s * 0.66, s * 0.52], fill=(255, 230, 210, 255))
d.ellipse([s * 0.14, s * 0.58, s * 0.86, s * 1.3], fill=(255, 230, 210, 255))
return img
def compose(frame: Image.Image, shape: str) -> Image.Image:
s = HOLE * 2
layer = Image.new("RGBA", frame.size, (0, 0, 0, 0))
layer.paste(avatar(s), (CX - s // 2, CY - s // 2), shape_mask(shape, s))
layer.alpha_composite(frame)
return layer
def font(size):
for name in ("C:/Windows/Fonts/arialbd.ttf", "C:/Windows/Fonts/segoeuib.ttf"):
try:
return ImageFont.truetype(name, size)
except OSError:
pass
return None
def background(w, h):
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))
return bg
def main() -> None:
name = sys.argv[1] if len(sys.argv) > 1 else "cat"
frame = Image.open(FRAMES / (name + ".png")).convert("RGBA")
big = background(1080, 1350)
big.alpha_composite(compose(frame, "flower").resize((900, 900), Image.LANCZOS), (90, 90))
d = ImageDraw.Draw(big)
d.text((540, 1090), "Рамка на форме «Цветочек»", font=font(54), fill=(40, 30, 40, 255), anchor="mm")
d.text((540, 1170), "аватарка встаёт в окно рамки", font=font(46), fill=(150, 90, 110, 255), anchor="mm")
big.convert("RGB").save(FRAMES / (name + "_on_flower.png"))
shapes = ("circle", "flower", "squircle", "hexagon", "heart", "triangle")
labels = ("Круг", "Цветочек", "Квадрат", "Шестиугольник", "Сердечко", "Треугольник")
tile = 560
sheet = background(tile * 3, tile * 2 + 40)
d = ImageDraw.Draw(sheet)
for i, (shape, label) in enumerate(zip(shapes, labels)):
x, y = (i % 3) * tile, (i // 3) * tile
sheet.alpha_composite(compose(frame, shape).resize((tile, tile), Image.LANCZOS), (x, y))
d.text((x + tile / 2, y + tile - 8), label, font=font(34), fill=(40, 30, 40, 255), anchor="mm")
sheet.convert("RGB").save(FRAMES / (name + "_on_shapes.png"))
print(FRAMES / (name + "_on_flower.png"))
if __name__ == "__main__":
main()