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>
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
"""Pictures that members make themselves (avatar frames, chat pictures): the safe handling of the bytes.
|
|
|
|
Nothing a member sends is kept as it came: the picture is decoded, cropped, scaled and written again as a fresh PNG, so
|
|
metadata, odd chunks and anything that is not pixels is gone. What is left is judged by the AI and by the owner.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import re
|
|
|
|
UID = re.compile(r"[1-9][0-9]{4,24}\Z")
|
|
INSTALL = re.compile(r"[a-f0-9]{64}\Z")
|
|
|
|
|
|
def valid_uid(value):
|
|
return isinstance(value, str) and UID.fullmatch(value) is not None
|
|
|
|
|
|
MAX_UPLOAD = 400_000
|
|
MAX_PIXELS = 16_000_000
|
|
FRAME_SIDE = 256
|
|
BUBBLE_SIDE = 512
|
|
PET_SIDE = 512 # the picture of a pair's "Серийчик" (the streak pet): kept whole, at most this wide or tall
|
|
MAX_PNG = 150 * 1024
|
|
|
|
|
|
def normalize_png(data: bytes, side: int, square: bool, limit: int = MAX_PNG) -> bytes:
|
|
"""A fresh PNG of at most `side` pixels (exactly side x side when `square`, cropped in the middle), at most `limit` bytes."""
|
|
from PIL import Image
|
|
|
|
if not isinstance(data, (bytes, bytearray)) or not 0 < len(data) <= MAX_UPLOAD:
|
|
raise ValueError("The picture is too large")
|
|
try:
|
|
image = Image.open(io.BytesIO(data))
|
|
if image.width * image.height > MAX_PIXELS:
|
|
raise ValueError("The picture is too large")
|
|
image.load()
|
|
except ValueError:
|
|
raise
|
|
except Exception:
|
|
raise ValueError("Not a picture")
|
|
image = image.convert("RGBA")
|
|
if square:
|
|
edge = min(image.width, image.height)
|
|
left, top = (image.width - edge) // 2, (image.height - edge) // 2
|
|
image = image.crop((left, top, left + edge, top + edge)).resize((side, side), Image.LANCZOS)
|
|
else:
|
|
image.thumbnail((side, side), Image.LANCZOS)
|
|
for colors in (0, 256, 128, 64):
|
|
shown = image if colors == 0 else image.quantize(colors=colors, method=Image.FASTOCTREE)
|
|
out = io.BytesIO()
|
|
shown.save(out, "PNG", optimize=True)
|
|
if out.tell() <= limit:
|
|
return out.getvalue()
|
|
raise ValueError("The picture is too complicated")
|
|
|
|
|
|
def clean_name(name, default="Без названия", limit=30) -> str:
|
|
"""A name for the screen: no control characters, no line breaks, at most `limit` characters."""
|
|
text = "".join(ch for ch in (name if isinstance(name, str) else "") if ch >= " " and ch != "\x7f")
|
|
text = " ".join(text.split())[:limit].strip()
|
|
return text or default
|