From e322556ecc5f29f925c3cf1a0e3654ff99eac070 Mon Sep 17 00:00:00 2001 From: bibed234 Date: Sun, 15 Nov 2026 17:03:22 +0200 Subject: [PATCH 1/3] Remove util folder and ensure map spans full Shorts width --- util/key_man.bat | 44 -------------------------------------------- util/key_man.command | 39 --------------------------------------- yt-dlp_serv.py | 4 ++-- 3 files changed, 2 insertions(+), 85 deletions(-) delete mode 100644 util/key_man.bat delete mode 100755 util/key_man.command diff --git a/util/key_man.bat b/util/key_man.bat deleted file mode 100644 index d45c385..0000000 --- a/util/key_man.bat +++ /dev/null @@ -1,44 +0,0 @@ -@echo off - - -:menu -color 07 -cls -echo =================================== -echo SELECT OPTION -echo 1 - Add key 2 - Delete key -echo =================================== - -set /p user_input="Enter Option: " - -if %user_input% == 1 goto key_adding -if %user_input% == 2 goto key_deleting - - -color 0C -echo. -echo ERROR: Please enter a correct option (1 or 2). -pause -goto menu - - -:key_adding -cls -set /p user_key="Enter your Google AI Studio key: " -setx GEMINI_API_KEY "%user_key%" -cls -echo =================================== -echo KEY ADDED ^| NOW YOU CAN LAUNCH AM -echo =================================== -pause -exit - -:key_deleting -reg delete "HKCU\Environment" /v GEMINI_API_KEY -cls -color 0A -echo =================================== -echo KEY DELETED -echo =================================== -pause -exit \ No newline at end of file diff --git a/util/key_man.command b/util/key_man.command deleted file mode 100755 index 826efb5..0000000 --- a/util/key_man.command +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - - -clear -echo "===================================" -echo " SELECT OPTION (macOS) " -echo " 1 - Add key 2 - Delete key " -echo "===================================" - -read -p "Enter Option: " user_input - -if [[ "$user_input" == "1" ]]; then - clear - echo "Enter your Google AI Studio key:" - read user_key - - if [[ -z "$user_key" ]]; then - echo "Error: Key cannot be empty." - exit 1 - fi - - echo "export GEMINI_API_KEY='$user_key'" >> ~/.zshrc - - export GEMINI_API_KEY="$user_key" - - echo "===================================" - echo " KEY ADDED TO .zshrc " - echo " RESTART YOUR TERMINAL/IDE! " - echo "===================================" - -elif [[ "$user_input" == "2" ]]; then - - sed -i '' '/export GEMINI_API_KEY=/d' ~/.zshrc - echo "===================================" - echo " KEY DELETED " - echo "===================================" -else - echo "Invalid option." -fi \ No newline at end of file diff --git a/yt-dlp_serv.py b/yt-dlp_serv.py index 48d0ca3..37ba122 100644 --- a/yt-dlp_serv.py +++ b/yt-dlp_serv.py @@ -1602,8 +1602,8 @@ def _decode_optional_image(field: str, label: str): branded = bool(watermark_image is not None or watermark_text or has_stars) map_bottom = 1450 if watermark_image is not None else (1664 if branded else 1760) map_area_height = max(240, map_bottom - map_top) - # Fit both dimensions so the whole map stays inside the space below the title. - scale = min(1040 / map_clip.w, map_area_height / map_clip.h) * 1.14 + # Fit inside the map area while ensuring the map always spans the full Shorts width. + scale = max(canvas_w / map_clip.w, min(1040 / map_clip.w, map_area_height / map_clip.h) * 1.14) map_clip = map_clip.resized(scale) map_y = map_top + (map_area_height - map_clip.h) / 2 if effects: From 40b4a5358a117ec30aac502c4ba79334db7c9f90 Mon Sep 17 00:00:00 2001 From: bibed234 Date: Sun, 15 Nov 2026 17:25:43 +0200 Subject: [PATCH 2/3] Split yt-dlp_serv.py into video_text, video_stickers, and video_render modules --- Dockerfile.ytserver | 2 +- video_render.py | 118 ++++++ video_stickers.py | 611 +++++++++++++++++++++++++++++ video_text.py | 185 +++++++++ yt-dlp_serv.py | 923 ++------------------------------------------ 5 files changed, 938 insertions(+), 901 deletions(-) create mode 100644 video_render.py create mode 100644 video_stickers.py create mode 100644 video_text.py diff --git a/Dockerfile.ytserver b/Dockerfile.ytserver index 62212d8..6f279a1 100644 --- a/Dockerfile.ytserver +++ b/Dockerfile.ytserver @@ -15,7 +15,7 @@ COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -COPY yt-dlp_serv.py sticker_themes.py extra_stickers.py server_runtime.py render_progress.py ./ +COPY yt-dlp_serv.py sticker_themes.py extra_stickers.py server_runtime.py render_progress.py video_text.py video_stickers.py video_render.py ./ COPY resources/fonts/ resources/fonts/ EXPOSE 8080 diff --git a/video_render.py b/video_render.py new file mode 100644 index 0000000..edf5abc --- /dev/null +++ b/video_render.py @@ -0,0 +1,118 @@ +import os +import subprocess + +def _apply_clip_fade_in(clip, total_duration: float, fade_start: float = 0.0, fade_duration: float = 0.6): + """Smoothly fade a clip's alpha mask in starting at ``fade_start`` over ``fade_duration`` seconds.""" + import numpy as np + from moviepy import VideoClip + + if fade_start <= 0.0 and fade_duration <= 0.0: + return clip.with_duration(total_duration) + base_frame = clip.get_frame(0) + if base_frame.ndim == 3 and base_frame.shape[2] == 4: + base_mask = base_frame[:, :, 3].astype(np.float32) / 255.0 + elif clip.mask is not None: + base_mask = clip.mask.get_frame(0).astype(np.float32) + else: + base_mask = np.ones((clip.h, clip.w), dtype=np.float32) + + def _mask_frame(t: float) -> np.ndarray: + if t < fade_start: + return np.zeros_like(base_mask) + if fade_duration <= 0.0 or t >= fade_start + fade_duration: + return base_mask + alpha = float((t - fade_start) / fade_duration) + return base_mask * max(0.0, min(1.0, alpha)) + + mask_clip = VideoClip(_mask_frame, is_mask=True, duration=total_duration) + return clip.with_duration(total_duration).with_mask(mask_clip) + + +def _x264_preset() -> str: + """x264 speed preset; ``VIDEO_X264_PRESET`` overrides the fast default.""" + allowed = ('ultrafast', 'superfast', 'veryfast', 'faster', 'fast', + 'medium', 'slow', 'slower', 'veryslow') + preset = os.environ.get('VIDEO_X264_PRESET', 'veryfast').strip().lower() + return preset if preset in allowed else 'veryfast' + + +def _render_threads() -> int: + """Encoder thread count; ``VIDEO_RENDER_THREADS`` overrides the CPU count.""" + try: + threads = int(os.environ.get('VIDEO_RENDER_THREADS', '0')) + except ValueError: + threads = 0 + return threads if threads > 0 else (os.cpu_count() or 2) + + +def _prepare_background(src: str, dst: str, duration: float, size=(1080, 1920), + effects: bool = True) -> None: + """Trim, cover-scale, center-crop and optionally soften the background in FFmpeg. + + This replaces per-frame MoviePy resizing and the Pillow blur (downscale to + a quarter, Gaussian blur radius 2, upscale) with one native pass. The + intermediate is encoded near-losslessly so the final encode sees the same + pixels the Python path produced. + """ + from moviepy.config import FFMPEG_BINARY + + w, h = size + filters = [f'scale={w}:{h}:force_original_aspect_ratio=increase:flags=lanczos', + f'crop={w}:{h}'] + if effects: + filters += [f'scale={w // 4}:{h // 4}:flags=bicubic', 'gblur=sigma=2', + f'scale={w}:{h}:flags=bicubic'] + filters.append('format=yuv420p') + command = [FFMPEG_BINARY, '-hide_banner', '-loglevel', 'error', '-nostdin', '-y', + '-i', src, '-t', f'{duration:.3f}', '-an', '-vf', ','.join(filters), + '-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '10', + '-threads', str(_render_threads()), dst] + result = subprocess.run(command, capture_output=True, text=True, timeout=600) + if result.returncode != 0: + raise RuntimeError(f'Background preprocessing failed: {result.stderr.strip()[-300:]}') + + +def _flatten_static_layers(clips, size, dynamic, duration): + """Merge consecutive static layers into single RGBA foreground clips. + + ``dynamic`` holds ids of clips that change over time (background, fades, + veil). Static runs between them are alpha-composited once with Pillow in + the same order and with the same positioning rules MoviePy uses, so each + output frame blends one foreground instead of many layers. + """ + import numpy as np + from PIL import Image + from moviepy import ImageClip + from moviepy.tools import compute_position + + out, canvas = [], None + + def flush(): + nonlocal canvas + if canvas is not None: + out.append(ImageClip(np.array(canvas)).with_duration(duration).with_position((0, 0))) + canvas = None + + for clip in clips: + if id(clip) in dynamic: + flush() + out.append(clip) + continue + frame = np.asarray(clip.get_frame(0)) + if frame.dtype != np.uint8: + frame = np.clip(frame, 0, 255).astype(np.uint8) + layer = Image.fromarray(frame[:, :, :3]).convert('RGBA') + if clip.mask is not None: + alpha = np.clip(np.asarray(clip.mask.get_frame(0)) * 255, 0, 255).astype(np.uint8) + layer.putalpha(Image.fromarray(alpha)) + elif frame.ndim == 3 and frame.shape[2] == 4: + layer.putalpha(Image.fromarray(frame[:, :, 3])) + pos = compute_position(layer.size, size, clip.pos(0), clip.relative_pos) + if canvas is None: + canvas = Image.new('RGBA', size, (0, 0, 0, 0)) + placed = Image.new('RGBA', size, (0, 0, 0, 0)) + placed.paste(layer, tuple(int(v) for v in pos)) + canvas = Image.alpha_composite(canvas, placed) + flush() + return out + diff --git a/video_stickers.py b/video_stickers.py new file mode 100644 index 0000000..20baef4 --- /dev/null +++ b/video_stickers.py @@ -0,0 +1,611 @@ +import os +import re + +from video_text import TITLE_FONT_PATH, _with_shadow + +from sticker_themes import ( + ALLOWED_STICKERS, + TWEMOJI_CODEPOINTS, + AUTO_STICKER_THEMES as _AUTO_STICKER_THEMES, + EXACT_WORD_KEYWORDS as _EXACT_WORD_KEYWORDS, +) + + +def _resolve_auto_stickers(title: str) -> tuple[str, str]: + """Choose a thematic (left, right) sticker pair based on English title keywords.""" + lowered = (title or '').lower() + if not lowered.strip(): + return ('globe', 'pin') + best_pair: tuple[str, str] | None = None + best_count = 0 + # Score each theme by distinct keyword hits; ties keep the earlier (higher-priority) theme. + for keywords, pair in _AUTO_STICKER_THEMES: + count = 0 + for kw in keywords: + if kw in _EXACT_WORD_KEYWORDS: + pattern = rf'(? best_count: + best_count = count + best_pair = pair + return best_pair if best_pair is not None else ('globe', 'pin') + + +def _star_polygon(cx: float, cy: float, outer_r: float, inner_r: float) -> list[tuple[float, float]]: + """Return the 10 vertices of a 5-pointed star centered at (cx, cy).""" + import math + + points: list[tuple[float, float]] = [] + for i in range(10): + angle = -math.pi / 2 + i * (math.pi / 5) + r = outer_r if i % 2 == 0 else inner_r + points.append((cx + r * math.cos(angle), cy + r * math.sin(angle))) + return points + + +def _draw_heart(draw, cx: float, cy: float, size: float, fill: tuple[int, int, int, int], + outline: tuple[int, int, int, int] = (0, 0, 0, 255), width: int = 4) -> None: + """Draw a smooth vector heart icon centered at (cx, cy).""" + import math + + pts: list[tuple[float, float]] = [] + for step in range(64): + t = (step / 64.0) * 2.0 * math.pi + x = 16.0 * (math.sin(t) ** 3) + y = -(13.0 * math.cos(t) - 5.0 * math.cos(2 * t) - 2.0 * math.cos(3 * t) - math.cos(4 * t)) + pts.append((cx + x * (size / 34.0), cy + y * (size / 34.0))) + draw.polygon(pts, fill=fill, outline=outline, width=width) + + +def _subscribe_badge_image( + text: str = 'Subscribe!', + font_path: str = TITLE_FONT_PATH, + effects: bool = True, +): + """Render the dark rounded '[ ❤️ Subscribe! ▶️ ]' call-to-action pill badge.""" + import numpy as np + from PIL import Image, ImageDraw, ImageFont + + label = (text or 'Subscribe!').strip()[:40] or 'Subscribe!' + scale = 2 + font_size = 52 * scale + font = ImageFont.truetype(font_path, font_size) + measure = ImageDraw.Draw(Image.new('RGBA', (1, 1))) + bbox = measure.textbbox((0, 0), label, font=font, stroke_width=3 * scale) + text_w = bbox[2] - bbox[0] + + heart_slot = 64 * scale + play_w, play_h = 68 * scale, 48 * scale + pad_x = 28 * scale + gap = 18 * scale + pill_h = 84 * scale + pill_w = pad_x * 2 + heart_slot + gap * 2 + text_w + play_w + + canvas = Image.new('RGBA', (pill_w + 16 * scale, pill_h + 16 * scale), (0, 0, 0, 0)) + draw = ImageDraw.Draw(canvas) + ox, oy = 8 * scale, 8 * scale + + # Rounded dark charcoal pill with subtle glossy border + draw.rounded_rectangle( + (ox, oy, ox + pill_w, oy + pill_h), + radius=26 * scale, + fill=(28, 30, 36, 228), + outline=(255, 255, 255, 58), + width=2 * scale, + ) + + # Left icon: glossy red heart + heart_cx = ox + pad_x + heart_slot / 2 + heart_cy = oy + pill_h / 2 + 2 * scale + _draw_heart( + draw, + heart_cx, + heart_cy, + 54 * scale, + fill=(255, 42, 75, 255), + outline=(15, 15, 20, 255), + width=3 * scale, + ) + # Specular highlight on top-left lobe of heart + draw.ellipse( + (heart_cx - 16 * scale, heart_cy - 16 * scale, heart_cx - 6 * scale, heart_cy - 7 * scale), + fill=(255, 190, 200, 190), + ) + + # Center text: bold white Subscribe! text with dark stroke + text_x = ox + pad_x + heart_slot + gap + text_w / 2 + text_y = oy + pill_h / 2 + draw.text( + (text_x, text_y), + label, + font=font, + anchor='mm', + fill=(255, 255, 255, 255), + stroke_width=3 * scale, + stroke_fill=(0, 0, 0, 240), + ) + + # Right icon: red YouTube-style rounded play button + play_x0 = ox + pad_x + heart_slot + gap * 2 + text_w + play_y0 = oy + (pill_h - play_h) / 2 + draw.rounded_rectangle( + (play_x0, play_y0, play_x0 + play_w, play_y0 + play_h), + radius=12 * scale, + fill=(255, 32, 32, 255), + outline=(15, 15, 20, 255), + width=3 * scale, + ) + tri_cx = play_x0 + play_w / 2 + 2 * scale + tri_cy = play_y0 + play_h / 2 + tri = [ + (tri_cx - 11 * scale, tri_cy - 12 * scale), + (tri_cx - 11 * scale, tri_cy + 12 * scale), + (tri_cx + 13 * scale, tri_cy), + ] + draw.polygon(tri, fill=(255, 255, 255, 255)) + + final_img = canvas.resize((canvas.width // scale, canvas.height // scale), Image.Resampling.LANCZOS) + if effects: + final_img = _with_shadow(final_img) + return np.array(final_img) + + +def _stars_row_image(color: str = '#ffffff', effects: bool = True): + """Render three 5-pointed stars ('★ ★ ★') for the bottom watermark footer.""" + import numpy as np + from PIL import Image, ImageColor, ImageDraw + + if not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color): + color = '#ffffff' + rgb = ImageColor.getrgb(color) + + scale = 2 + w, h = 320 * scale, 76 * scale + canvas = Image.new('RGBA', (w, h), (0, 0, 0, 0)) + draw = ImageDraw.Draw(canvas) + + centers_x = (w * 0.22, w * 0.50, w * 0.78) + cy = h * 0.52 + for cx in centers_x: + pts = _star_polygon(cx, cy, outer_r=26 * scale, inner_r=11 * scale) + draw.polygon( + pts, + fill=rgb + (255,), + outline=(0, 0, 0, 240) if effects else rgb + (220,), + width=4 * scale if effects else 2 * scale, + ) + + final_img = canvas.resize((w // scale, h // scale), Image.Resampling.LANCZOS) + if effects: + final_img = _with_shadow(final_img) + return np.array(final_img) + + +def _parse_sticker_spec(raw_kind: str, title: str = '', side: str = 'left') -> tuple[str | None, str]: + """Parse a sticker specification into ``(gen_style_or_none, base_kind)``.""" + raw = (raw_kind or 'none').strip().lower() + gen_style: str | None = None + if ':' in raw: + prefix, _, base_kind = raw.partition(':') + if prefix in ('gen', 'gen_diecut', 'gen_neon', 'gen_stamp'): + gen_style = 'diecut' if prefix == 'gen' else prefix.removeprefix('gen_') + raw = base_kind or 'auto' + if raw == 'auto': + left_auto, right_auto = _resolve_auto_stickers(title) + raw = right_auto if side == 'right' else left_auto + return gen_style, raw + + +def _style_generated_sticker(icon_canvas, gen_style: str, size: int): + """Wrap a procedural vector icon in a generated sticker style (die-cut, neon, or stamp).""" + from PIL import Image, ImageDraw, ImageFilter + + s = icon_canvas.width + if gen_style == 'diecut': + # Create a crisp white vinyl die-cut sticker border around the vector silhouette. + alpha = icon_canvas.getchannel('A') + dilate = max(3, int(round(s * 0.055))) | 1 + border_mask = alpha.filter(ImageFilter.MaxFilter(dilate)) + vinyl = Image.new('RGBA', (s, s), (255, 255, 255, 0)) + vinyl.putalpha(border_mask) + white_layer = Image.new('RGBA', (s, s), (252, 253, 255, 255)) + white_layer.putalpha(border_mask) + styled = Image.alpha_composite(white_layer, icon_canvas) + styled = styled.rotate(6, resample=Image.Resampling.BICUBIC, expand=False) + return styled.resize((size, size), Image.Resampling.LANCZOS) + + if gen_style == 'neon': + badge = Image.new('RGBA', (s, s), (0, 0, 0, 0)) + glow = Image.new('RGBA', (s, s), (0, 0, 0, 0)) + gdraw = ImageDraw.Draw(glow) + pad = int(s * 0.08) + gdraw.ellipse((pad, pad, s - pad, s - pad), outline=(56, 189, 248, 230), width=int(s * 0.055)) + glow = glow.filter(ImageFilter.GaussianBlur(max(4, s // 28))) + bdraw = ImageDraw.Draw(badge) + bdraw.ellipse((pad, pad, s - pad, s - pad), fill=(15, 23, 42, 238), outline=(99, 102, 241, 255), width=int(s * 0.032)) + bdraw.ellipse((pad + 6, pad + 6, s - pad - 6, s - pad - 6), outline=(56, 189, 248, 210), width=max(2, s // 70)) + badge = Image.alpha_composite(glow, badge) + inner_size = int(s * 0.68) + icon_small = icon_canvas.resize((inner_size, inner_size), Image.Resampling.LANCZOS) + offset = (s - inner_size) // 2 + badge.alpha_composite(icon_small, (offset, offset)) + return badge.resize((size, size), Image.Resampling.LANCZOS) + + # 'stamp': tilted editorial badge with double accent frame and corner sparkles + stamp = Image.new('RGBA', (s, s), (0, 0, 0, 0)) + sdraw = ImageDraw.Draw(stamp) + pad = int(s * 0.08) + sdraw.rounded_rectangle((pad, pad, s - pad, s - pad), radius=int(s * 0.22), + fill=(30, 27, 75, 235), outline=(251, 191, 36, 255), width=int(s * 0.03)) + sdraw.rounded_rectangle((pad + 10, pad + 10, s - pad - 10, s - pad - 10), radius=int(s * 0.17), + outline=(255, 255, 255, 110), width=max(2, s // 80)) + # Tiny decorative sparkle stars in top corners + for sx, sy in ((s * 0.22, s * 0.22), (s * 0.78, s * 0.22)): + sdraw.polygon(_star_polygon(sx, sy, s * 0.055, s * 0.022), fill=(253, 224, 71, 240)) + inner_size = int(s * 0.68) + icon_small = icon_canvas.resize((inner_size, inner_size), Image.Resampling.LANCZOS) + offset = (s - inner_size) // 2 + stamp.alpha_composite(icon_small, (offset, offset + int(s * 0.03))) + stamp = stamp.rotate(-6, resample=Image.Resampling.BICUBIC, expand=False) + return stamp.resize((size, size), Image.Resampling.LANCZOS) + + +from extra_stickers import draw_extra_sticker + + +def _draw_extra_sticker(kind, draw, s, outline, ow): + """Delegate extended procedural sticker drawing to extra_stickers.py.""" + return draw_extra_sticker(kind, draw, s, outline, ow, star_polygon_fn=_star_polygon) + + +def _sticker_image(kind: str, size: int = 140, effects: bool = True, allow_remote: bool = False): + """Render a decorative top-corner sticker (library Twemoji or procedural generated style).""" + import numpy as np + from PIL import Image, ImageDraw, ImageOps + + gen_style, kind = _parse_sticker_spec(kind) + if kind not in ALLOWED_STICKERS or kind == 'none': + return None + + # Library stickers (without a gen_ prefix) use Twemoji cache/CDN when available. + codepoint = None if gen_style else TWEMOJI_CODEPOINTS.get(kind) + if codepoint: + stickers_dir = os.path.join(STORAGE_DIR, 'stickers') + cache_path = os.path.join(stickers_dir, f'{kind}.png') + if not os.path.isfile(cache_path) and allow_remote: + try: + import requests + os.makedirs(stickers_dir, exist_ok=True) + url = f'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/72x72/{codepoint}.png' + resp = requests.get(url, timeout=1.5) + if resp.status_code == 200 and resp.content: + with Image.open(io.BytesIO(resp.content)) as fetched: + fetched.convert('RGBA').save(cache_path, format='PNG') + except Exception: + pass + if os.path.isfile(cache_path): + try: + with Image.open(cache_path) as cached: + final_img = ImageOps.contain(cached.convert('RGBA'), (size, size), method=Image.Resampling.LANCZOS) + if final_img.size != (size, size): + padded = Image.new('RGBA', (size, size), (0, 0, 0, 0)) + padded.paste(final_img, ((size - final_img.width) // 2, (size - final_img.height) // 2)) + final_img = padded + if effects: + final_img = _with_shadow(final_img) + return np.array(final_img) + except Exception: + pass + + scale = 2 + s = size * scale + canvas = Image.new('RGBA', (s, s), (0, 0, 0, 0)) + draw = ImageDraw.Draw(canvas) + outline = (18, 18, 24, 255) + ow = 4 * scale + + if _draw_extra_sticker(kind, draw, s, outline, ow): + pass + elif kind == 'santa': + # Fluffy white beard base + draw.ellipse((s * 0.16, s * 0.44, s * 0.84, s * 0.94), fill=(250, 252, 255, 255), outline=outline, width=ow) + draw.ellipse((s * 0.12, s * 0.52, s * 0.46, s * 0.84), fill=(250, 252, 255, 255), outline=outline, width=ow) + draw.ellipse((s * 0.54, s * 0.52, s * 0.88, s * 0.84), fill=(250, 252, 255, 255), outline=outline, width=ow) + # Warm face + draw.ellipse((s * 0.24, s * 0.30, s * 0.76, s * 0.72), fill=(255, 214, 186, 255), outline=outline, width=ow) + # Re-fill inner beard over lower face + draw.chord((s * 0.18, s * 0.42, s * 0.82, s * 0.92), start=0, end=180, fill=(250, 252, 255, 255), outline=outline, width=ow) + # Rosy cheeks & eyes + draw.ellipse((s * 0.28, s * 0.48, s * 0.39, s * 0.56), fill=(255, 138, 138, 210)) + draw.ellipse((s * 0.61, s * 0.48, s * 0.72, s * 0.56), fill=(255, 138, 138, 210)) + draw.ellipse((s * 0.35, s * 0.41, s * 0.42, s * 0.48), fill=outline) + draw.ellipse((s * 0.58, s * 0.41, s * 0.65, s * 0.48), fill=outline) + # Mustache & rosy nose + draw.ellipse((s * 0.30, s * 0.53, s * 0.52, s * 0.65), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.48, s * 0.53, s * 0.70, s * 0.65), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.44, s * 0.47, s * 0.56, s * 0.57), fill=(244, 92, 92, 255), outline=outline, width=ow // 2) + # Red Santa hat & white fur brim + pom-pom + hat_pts = [(s * 0.18, s * 0.34), (s * 0.52, s * 0.06), (s * 0.86, s * 0.28), (s * 0.80, s * 0.36)] + draw.polygon(hat_pts, fill=(228, 34, 46, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.15, s * 0.27, s * 0.83, s * 0.40), radius=14 * scale, fill=(252, 253, 255, 255), outline=outline, width=ow) + draw.ellipse((s * 0.76, s * 0.18, s * 0.94, s * 0.36), fill=(252, 253, 255, 255), outline=outline, width=ow) + + elif kind == 'gift': + # Candy cane behind top-right of gift box + draw.arc((s * 0.52, s * 0.06, s * 0.90, s * 0.46), start=180, end=20, fill=(235, 38, 50, 255), width=12 * scale) + # Golden ribbon bow Loops on top + draw.ellipse((s * 0.24, s * 0.14, s * 0.52, s * 0.38), fill=(255, 204, 42, 255), outline=outline, width=ow) + draw.ellipse((s * 0.48, s * 0.14, s * 0.76, s * 0.38), fill=(255, 204, 42, 255), outline=outline, width=ow) + # Red gift box body + draw.rounded_rectangle((s * 0.18, s * 0.36, s * 0.82, s * 0.90), radius=12 * scale, fill=(224, 36, 48, 255), outline=outline, width=ow) + # Gift box lid + draw.rounded_rectangle((s * 0.14, s * 0.28, s * 0.86, s * 0.44), radius=10 * scale, fill=(242, 52, 64, 255), outline=outline, width=ow) + # Vertical & horizontal gold ribbon stripes + draw.rectangle((s * 0.44, s * 0.28, s * 0.56, s * 0.90), fill=(255, 208, 46, 255), outline=outline, width=ow // 2) + draw.rectangle((s * 0.18, s * 0.58, s * 0.82, s * 0.69), fill=(255, 208, 46, 255), outline=outline, width=ow // 2) + # Holly leaves & berry accent + draw.ellipse((s * 0.64, s * 0.20, s * 0.88, s * 0.34), fill=(40, 184, 86, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.44, s * 0.22, s * 0.56, s * 0.34), fill=(255, 230, 90, 255), outline=outline, width=ow // 2) + + elif kind == 'snowflake': + import math + draw.ellipse((s * 0.14, s * 0.14, s * 0.86, s * 0.86), fill=(48, 168, 255, 255), outline=outline, width=ow) + cx, cy = s * 0.50, s * 0.50 + for i in range(6): + ang = i * (math.pi / 3) + x2 = cx + s * 0.30 * math.cos(ang) + y2 = cy + s * 0.30 * math.sin(ang) + draw.line((cx, cy, x2, y2), fill=(255, 255, 255, 255), width=6 * scale) + bx = cx + s * 0.19 * math.cos(ang) + by = cy + s * 0.19 * math.sin(ang) + for delta in (-0.55, 0.55): + tx = bx + s * 0.09 * math.cos(ang + delta) + ty = by + s * 0.09 * math.sin(ang + delta) + draw.line((bx, by, tx, ty), fill=(255, 255, 255, 255), width=4 * scale) + draw.ellipse((s * 0.42, s * 0.42, s * 0.58, s * 0.58), fill=(225, 248, 255, 255), outline=outline, width=ow // 2) + + elif kind == 'pumpkin': + # Stem + draw.polygon([(s * 0.45, s * 0.10), (s * 0.57, s * 0.10), (s * 0.54, s * 0.28), (s * 0.46, s * 0.28)], + fill=(46, 164, 72, 255), outline=outline, width=ow) + # Ribbed orange pumpkin lobes + draw.ellipse((s * 0.12, s * 0.24, s * 0.58, s * 0.88), fill=(242, 116, 28, 255), outline=outline, width=ow) + draw.ellipse((s * 0.42, s * 0.24, s * 0.88, s * 0.88), fill=(242, 116, 28, 255), outline=outline, width=ow) + draw.ellipse((s * 0.26, s * 0.22, s * 0.74, s * 0.90), fill=(255, 138, 36, 255), outline=outline, width=ow) + # Glowing jack-o'-lantern eyes & grin + draw.polygon([(s * 0.30, s * 0.48), (s * 0.40, s * 0.36), (s * 0.45, s * 0.48)], fill=(255, 232, 64, 255), outline=outline, width=ow // 2) + draw.polygon([(s * 0.55, s * 0.48), (s * 0.60, s * 0.36), (s * 0.70, s * 0.48)], fill=(255, 232, 64, 255), outline=outline, width=ow // 2) + draw.polygon([(s * 0.28, s * 0.64), (s * 0.50, s * 0.72), (s * 0.72, s * 0.64), (s * 0.64, s * 0.78), (s * 0.36, s * 0.78)], + fill=(255, 232, 64, 255), outline=outline, width=ow // 2) + + elif kind == 'globe': + draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(36, 146, 245, 255), outline=outline, width=ow) + # Stylized green continents + draw.polygon([(s * 0.26, s * 0.30), (s * 0.46, s * 0.24), (s * 0.48, s * 0.46), (s * 0.34, s * 0.66), (s * 0.22, s * 0.50)], fill=(58, 204, 112, 255), outline=outline, width=ow // 2) + draw.polygon([(s * 0.56, s * 0.28), (s * 0.78, s * 0.36), (s * 0.74, s * 0.68), (s * 0.54, s * 0.58)], fill=(58, 204, 112, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), outline=outline, width=ow) + + elif kind == 'compass': + draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(245, 194, 48, 255), outline=outline, width=ow) + draw.ellipse((s * 0.20, s * 0.20, s * 0.80, s * 0.80), fill=(30, 42, 62, 255), outline=outline, width=ow // 2) + # North red pointer & South white pointer + draw.polygon([(s * 0.50, s * 0.24), (s * 0.60, s * 0.50), (s * 0.40, s * 0.50)], fill=(244, 54, 68, 255), outline=outline, width=ow // 2) + draw.polygon([(s * 0.50, s * 0.76), (s * 0.60, s * 0.50), (s * 0.40, s * 0.50)], fill=(245, 248, 255, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.45, s * 0.45, s * 0.55, s * 0.55), fill=(255, 214, 64, 255), outline=outline, width=ow // 2) + + elif kind == 'pin': + draw.polygon([(s * 0.24, s * 0.46), (s * 0.50, s * 0.90), (s * 0.76, s * 0.46)], fill=(238, 42, 62, 255), outline=outline, width=ow) + draw.ellipse((s * 0.20, s * 0.12, s * 0.80, s * 0.68), fill=(238, 42, 62, 255), outline=outline, width=ow) + draw.ellipse((s * 0.36, s * 0.26, s * 0.64, s * 0.54), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) + + elif kind == 'plane': + # Sky badge + white/blue jet plane + draw.ellipse((s * 0.14, s * 0.14, s * 0.86, s * 0.86), fill=(46, 156, 255, 255), outline=outline, width=ow) + wings = [(s * 0.18, s * 0.56), (s * 0.82, s * 0.24), (s * 0.64, s * 0.82), (s * 0.48, s * 0.58), (s * 0.32, s * 0.68)] + draw.polygon(wings, fill=(250, 252, 255, 255), outline=outline, width=ow) + + elif kind == 'money': + # Tie top + plump golden money sack + draw.polygon([(s * 0.36, s * 0.14), (s * 0.64, s * 0.14), (s * 0.58, s * 0.30), (s * 0.42, s * 0.30)], + fill=(238, 188, 52, 255), outline=outline, width=ow) + draw.ellipse((s * 0.18, s * 0.26, s * 0.82, s * 0.90), fill=(246, 198, 58, 255), outline=outline, width=ow) + draw.ellipse((s * 0.34, s * 0.44, s * 0.66, s * 0.76), fill=(46, 192, 96, 255), outline=outline, width=ow // 2) + + elif kind == 'chart': + draw.rounded_rectangle((s * 0.12, s * 0.14, s * 0.88, s * 0.88), radius=14 * scale, fill=(245, 248, 255, 255), outline=outline, width=ow) + draw.rectangle((s * 0.22, s * 0.56, s * 0.36, s * 0.80), fill=(52, 152, 245, 255), outline=outline, width=ow // 2) + draw.rectangle((s * 0.43, s * 0.40, s * 0.57, s * 0.80), fill=(46, 204, 113, 255), outline=outline, width=ow // 2) + draw.rectangle((s * 0.64, s * 0.24, s * 0.78, s * 0.80), fill=(255, 196, 42, 255), outline=outline, width=ow // 2) + + elif kind == 'diamond': + gem = [(s * 0.28, s * 0.18), (s * 0.72, s * 0.18), (s * 0.90, s * 0.40), (s * 0.50, s * 0.88), (s * 0.10, s * 0.40)] + draw.polygon(gem, fill=(56, 214, 255, 255), outline=outline, width=ow) + draw.polygon([(s * 0.28, s * 0.18), (s * 0.72, s * 0.18), (s * 0.64, s * 0.40), (s * 0.36, s * 0.40)], + fill=(165, 242, 255, 255), outline=outline, width=ow // 2) + + elif kind == 'shield': + pts = [(s * 0.18, s * 0.14), (s * 0.82, s * 0.14), (s * 0.82, s * 0.50), (s * 0.50, s * 0.90), (s * 0.18, s * 0.50)] + draw.polygon(pts, fill=(42, 118, 238, 255), outline=outline, width=ow) + inner = [(s * 0.28, s * 0.22), (s * 0.72, s * 0.22), (s * 0.72, s * 0.48), (s * 0.50, s * 0.78), (s * 0.28, s * 0.48)] + draw.polygon(inner, fill=(255, 204, 46, 255), outline=outline, width=ow // 2) + + elif kind == 'swords': + # Crossed silver blades with gold guards + draw.polygon([(s * 0.16, s * 0.22), (s * 0.24, s * 0.14), (s * 0.84, s * 0.74), (s * 0.76, s * 0.82)], + fill=(224, 232, 245, 255), outline=outline, width=ow) + draw.polygon([(s * 0.84, s * 0.22), (s * 0.76, s * 0.14), (s * 0.16, s * 0.74), (s * 0.24, s * 0.82)], + fill=(224, 232, 245, 255), outline=outline, width=ow) + draw.ellipse((s * 0.40, s * 0.40, s * 0.60, s * 0.60), fill=(255, 204, 46, 255), outline=outline, width=ow) + + elif kind == 'rocket': + # Exhaust flame + draw.polygon([(s * 0.38, s * 0.72), (s * 0.50, s * 0.94), (s * 0.62, s * 0.72)], fill=(255, 112, 34, 255), outline=outline, width=ow // 2) + # Side fins + draw.polygon([(s * 0.20, s * 0.74), (s * 0.34, s * 0.52), (s * 0.36, s * 0.74)], fill=(235, 44, 58, 255), outline=outline, width=ow) + draw.polygon([(s * 0.80, s * 0.74), (s * 0.66, s * 0.52), (s * 0.64, s * 0.74)], fill=(235, 44, 58, 255), outline=outline, width=ow) + # Rocket fuselage & nose cone + draw.polygon([(s * 0.50, s * 0.08), (s * 0.68, s * 0.34), (s * 0.66, s * 0.74), (s * 0.34, s * 0.74), (s * 0.32, s * 0.34)], + fill=(248, 250, 255, 255), outline=outline, width=ow) + draw.polygon([(s * 0.50, s * 0.08), (s * 0.66, s * 0.30), (s * 0.34, s * 0.30)], + fill=(235, 44, 58, 255), outline=outline, width=ow) + draw.ellipse((s * 0.40, s * 0.38, s * 0.60, s * 0.58), fill=(58, 198, 255, 255), outline=outline, width=ow // 2) + + elif kind == 'bolt': + bolt_pts = [ + (s * 0.56, s * 0.08), + (s * 0.22, s * 0.54), + (s * 0.48, s * 0.54), + (s * 0.38, s * 0.92), + (s * 0.78, s * 0.44), + (s * 0.52, s * 0.44), + ] + draw.polygon(bolt_pts, fill=(255, 214, 36, 255), outline=outline, width=ow) + + elif kind == 'fire': + outer_flame = [(s * 0.50, s * 0.08), (s * 0.82, s * 0.48), (s * 0.74, s * 0.84), (s * 0.50, s * 0.92), (s * 0.26, s * 0.84), (s * 0.18, s * 0.48)] + inner_flame = [(s * 0.50, s * 0.32), (s * 0.68, s * 0.58), (s * 0.62, s * 0.82), (s * 0.50, s * 0.88), (s * 0.38, s * 0.82), (s * 0.32, s * 0.58)] + draw.polygon(outer_flame, fill=(255, 86, 34, 255), outline=outline, width=ow) + draw.polygon(inner_flame, fill=(255, 214, 46, 255), outline=outline, width=ow // 2) + + elif kind == 'trophy': + # Handles + draw.ellipse((s * 0.12, s * 0.22, s * 0.40, s * 0.52), fill=(255, 204, 38, 255), outline=outline, width=ow) + draw.ellipse((s * 0.60, s * 0.22, s * 0.88, s * 0.52), fill=(255, 204, 38, 255), outline=outline, width=ow) + # Cup body + stem + base + draw.polygon([(s * 0.24, s * 0.16), (s * 0.76, s * 0.16), (s * 0.66, s * 0.58), (s * 0.34, s * 0.58)], + fill=(255, 214, 46, 255), outline=outline, width=ow) + draw.rectangle((s * 0.44, s * 0.56, s * 0.56, s * 0.76), fill=(235, 180, 28, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.28, s * 0.74, s * 0.72, s * 0.88), radius=8 * scale, fill=(215, 152, 24, 255), outline=outline, width=ow) + + elif kind == 'crown': + crown_pts = [ + (s * 0.14, s * 0.78), + (s * 0.10, s * 0.28), + (s * 0.34, s * 0.52), + (s * 0.50, s * 0.18), + (s * 0.66, s * 0.52), + (s * 0.90, s * 0.28), + (s * 0.86, s * 0.78), + ] + draw.polygon(crown_pts, fill=(255, 204, 38, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.14, s * 0.74, s * 0.86, s * 0.88), radius=8 * scale, fill=(235, 168, 20, 255), outline=outline, width=ow) + draw.ellipse((s * 0.45, s * 0.56, s * 0.55, s * 0.66), fill=(244, 42, 75, 255), outline=outline, width=ow // 2) + + elif kind == 'star': + pts = _star_polygon(s * 0.50, s * 0.52, outer_r=s * 0.40, inner_r=s * 0.18) + draw.polygon(pts, fill=(255, 216, 46, 255), outline=outline, width=ow) + + elif kind == 'heart': + _draw_heart(draw, s * 0.50, s * 0.52, s * 0.78, fill=(255, 42, 78, 255), outline=outline, width=ow) + + elif kind == 'skull': + draw.rounded_rectangle((s * 0.32, s * 0.56, s * 0.68, s * 0.88), radius=10 * scale, fill=(245, 247, 252, 255), outline=outline, width=ow) + draw.ellipse((s * 0.18, s * 0.14, s * 0.82, s * 0.74), fill=(245, 247, 252, 255), outline=outline, width=ow) + draw.ellipse((s * 0.28, s * 0.36, s * 0.44, s * 0.52), fill=outline) + draw.ellipse((s * 0.56, s * 0.36, s * 0.72, s * 0.52), fill=outline) + draw.polygon([(s * 0.50, s * 0.50), (s * 0.45, s * 0.60), (s * 0.55, s * 0.60)], fill=outline) + + elif kind == 'bear': + # Round ears + warm inner ears + draw.ellipse((s * 0.12, s * 0.12, s * 0.42, s * 0.42), fill=(142, 82, 42, 255), outline=outline, width=ow) + draw.ellipse((s * 0.58, s * 0.12, s * 0.88, s * 0.42), fill=(142, 82, 42, 255), outline=outline, width=ow) + draw.ellipse((s * 0.19, s * 0.19, s * 0.35, s * 0.35), fill=(216, 158, 108, 255)) + draw.ellipse((s * 0.65, s * 0.19, s * 0.81, s * 0.35), fill=(216, 158, 108, 255)) + # Plump brown bear head + draw.ellipse((s * 0.16, s * 0.22, s * 0.84, s * 0.88), fill=(158, 94, 48, 255), outline=outline, width=ow) + # Light tan muzzle + dark nose + draw.ellipse((s * 0.32, s * 0.48, s * 0.68, s * 0.78), fill=(234, 192, 146, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.42, s * 0.50, s * 0.58, s * 0.62), fill=outline) + # Eyes + draw.ellipse((s * 0.31, s * 0.38, s * 0.39, s * 0.46), fill=outline) + draw.ellipse((s * 0.61, s * 0.38, s * 0.69, s * 0.46), fill=outline) + + elif kind == 'paw': + draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(252, 211, 77, 255), outline=outline, width=ow) + pad_color = (120, 53, 15, 255) + draw.ellipse((s * 0.30, s * 0.48, s * 0.70, s * 0.78), fill=pad_color, outline=outline, width=ow // 2) + for px, py in ((0.24, 0.34), (0.39, 0.24), (0.61, 0.24), (0.76, 0.34)): + draw.ellipse((s * (px - 0.08), s * (py - 0.09), s * (px + 0.08), s * (py + 0.09)), fill=pad_color, outline=outline, width=ow // 2) + + elif kind == 'tree': + draw.rectangle((s * 0.42, s * 0.68, s * 0.58, s * 0.90), fill=(132, 78, 42, 255), outline=outline, width=ow) + draw.polygon([(s * 0.50, s * 0.34), (s * 0.86, s * 0.74), (s * 0.14, s * 0.74)], fill=(22, 163, 74, 255), outline=outline, width=ow) + draw.polygon([(s * 0.50, s * 0.20), (s * 0.80, s * 0.56), (s * 0.20, s * 0.56)], fill=(34, 197, 94, 255), outline=outline, width=ow) + draw.polygon([(s * 0.50, s * 0.08), (s * 0.72, s * 0.38), (s * 0.28, s * 0.38)], fill=(74, 222, 128, 255), outline=outline, width=ow) + + elif kind == 'wolf': + draw.polygon([(s * 0.18, s * 0.42), (s * 0.22, s * 0.10), (s * 0.42, s * 0.28)], fill=(100, 116, 139, 255), outline=outline, width=ow) + draw.polygon([(s * 0.82, s * 0.42), (s * 0.78, s * 0.10), (s * 0.58, s * 0.28)], fill=(100, 116, 139, 255), outline=outline, width=ow) + draw.polygon([(s * 0.16, s * 0.38), (s * 0.50, s * 0.22), (s * 0.84, s * 0.38), (s * 0.76, s * 0.68), (s * 0.50, s * 0.88), (s * 0.24, s * 0.68)], + fill=(148, 163, 184, 255), outline=outline, width=ow) + draw.polygon([(s * 0.34, s * 0.54), (s * 0.50, s * 0.88), (s * 0.66, s * 0.54)], fill=(241, 245, 249, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.32, s * 0.42, s * 0.42, s * 0.50), fill=(250, 204, 21, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.58, s * 0.42, s * 0.68, s * 0.50), fill=(250, 204, 21, 255), outline=outline, width=ow // 2) + draw.ellipse((s * 0.44, s * 0.74, s * 0.56, s * 0.84), fill=outline) + + elif kind == 'beer': + draw.rounded_rectangle((s * 0.62, s * 0.38, s * 0.86, s * 0.76), radius=12 * scale, outline=outline, width=ow) + draw.rounded_rectangle((s * 0.22, s * 0.28, s * 0.70, s * 0.88), radius=10 * scale, fill=(245, 158, 11, 255), outline=outline, width=ow) + for fx in (0.28, 0.44, 0.60): + draw.ellipse((s * (fx - 0.12), s * 0.14, s * (fx + 0.12), s * 0.38), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) + + elif kind == 'coffee': + draw.rounded_rectangle((s * 0.64, s * 0.42, s * 0.86, s * 0.70), radius=12 * scale, outline=outline, width=ow) + draw.chord((s * 0.18, s * 0.28, s * 0.74, s * 0.84), start=0, end=180, fill=(248, 250, 252, 255), outline=outline, width=ow) + draw.ellipse((s * 0.18, s * 0.32, s * 0.74, s * 0.48), fill=(120, 53, 15, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.12, s * 0.78, s * 0.82, s * 0.88), radius=6 * scale, fill=(226, 232, 240, 255), outline=outline, width=ow) + + elif kind == 'wine': + draw.polygon([(s * 0.28, s * 0.14), (s * 0.72, s * 0.14), (s * 0.64, s * 0.54), (s * 0.36, s * 0.54)], fill=(225, 29, 72, 255), outline=outline, width=ow) + draw.rectangle((s * 0.47, s * 0.54, s * 0.53, s * 0.82), fill=(226, 232, 240, 255), outline=outline, width=ow // 2) + draw.rounded_rectangle((s * 0.32, s * 0.80, s * 0.68, s * 0.88), radius=6 * scale, fill=(226, 232, 240, 255), outline=outline, width=ow) + + elif kind == 'pizza': + draw.polygon([(s * 0.50, s * 0.88), (s * 0.16, s * 0.22), (s * 0.84, s * 0.22)], fill=(250, 204, 21, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.14, s * 0.14, s * 0.86, s * 0.28), radius=10 * scale, fill=(217, 119, 6, 255), outline=outline, width=ow) + for px, py in ((0.40, 0.40), (0.58, 0.44), (0.48, 0.62)): + draw.ellipse((s * (px - 0.07), s * (py - 0.07), s * (px + 0.07), s * (py + 0.07)), fill=(220, 38, 38, 255), outline=outline, width=ow // 2) + + elif kind == 'soccer': + draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(248, 250, 252, 255), outline=outline, width=ow) + pent = _star_polygon(s * 0.50, s * 0.50, outer_r=s * 0.18, inner_r=s * 0.15) + draw.polygon(pent, fill=outline) + for vx, vy in pent[::2]: + draw.line((vx, vy, s * 0.50 + (vx - s * 0.50) * 2.0, s * 0.50 + (vy - s * 0.50) * 2.0), fill=outline, width=ow // 2) + + elif kind == 'car': + draw.polygon([(s * 0.24, s * 0.48), (s * 0.36, s * 0.28), (s * 0.66, s * 0.28), (s * 0.78, s * 0.48)], fill=(56, 189, 248, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.12, s * 0.46, s * 0.88, s * 0.74), radius=12 * scale, fill=(239, 68, 68, 255), outline=outline, width=ow) + draw.ellipse((s * 0.20, s * 0.62, s * 0.38, s * 0.80), fill=(30, 41, 59, 255), outline=outline, width=ow) + draw.ellipse((s * 0.62, s * 0.62, s * 0.80, s * 0.80), fill=(30, 41, 59, 255), outline=outline, width=ow) + + elif kind == 'brain': + for bx in (0.36, 0.64): + draw.ellipse((s * (bx - 0.22), s * 0.18, s * (bx + 0.22), s * 0.82), fill=(244, 114, 182, 255), outline=outline, width=ow) + draw.line((s * 0.50, s * 0.18, s * 0.50, s * 0.82), fill=outline, width=ow) + + elif kind == 'book': + draw.rounded_rectangle((s * 0.18, s * 0.58, s * 0.84, s * 0.82), radius=8 * scale, fill=(59, 130, 246, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.16, s * 0.38, s * 0.82, s * 0.62), radius=8 * scale, fill=(16, 185, 129, 255), outline=outline, width=ow) + draw.rounded_rectangle((s * 0.20, s * 0.18, s * 0.86, s * 0.42), radius=8 * scale, fill=(244, 63, 94, 255), outline=outline, width=ow) + + elif kind == 'sun': + pts = _star_polygon(s * 0.50, s * 0.50, outer_r=s * 0.42, inner_r=s * 0.28) + draw.polygon(pts, fill=(251, 146, 60, 255), outline=outline, width=ow) + draw.ellipse((s * 0.24, s * 0.24, s * 0.76, s * 0.76), fill=(250, 204, 21, 255), outline=outline, width=ow) + + if gen_style: + final_img = _style_generated_sticker(canvas, gen_style, size) + else: + final_img = canvas.resize((size, size), Image.Resampling.LANCZOS) + if effects: + final_img = _with_shadow(final_img) + return np.array(final_img) + diff --git a/video_text.py b/video_text.py new file mode 100644 index 0000000..b1ed267 --- /dev/null +++ b/video_text.py @@ -0,0 +1,185 @@ +import os +import re + +TITLE_FONT_PATH = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "resources", "fonts", "DIN Condensed.ttf", +) + + +def _title_settings(data): + font_name = str(data.get('title_font') or 'DIN Condensed.ttf') + font_dir = os.path.dirname(TITLE_FONT_PATH) + # Accept bundled filenames only, rather than arbitrary client-supplied paths. + fonts = {name for name in os.listdir(font_dir) if name.lower().endswith(('.ttf', '.otf'))} + if font_name not in fonts: + raise ValueError('Unknown title font.') + raw_size = data.get('title_font_size', 100) + # Reject booleans explicitly because int(True) would otherwise succeed. + if isinstance(raw_size, bool): + raise ValueError('Title size must be a number between 48 and 144.') + try: + size = int(raw_size) + except (TypeError, ValueError, OverflowError): + raise ValueError('Title size must be a number between 48 and 144.') + if not 48 <= size <= 144: + raise ValueError('Title size must be between 48 and 144.') + return os.path.join(font_dir, font_name), size + + +def _title_colors(data): + colors = (data.get('title_first_line_color', '#ffffff'), + data.get('title_other_lines_color', '#ffe735')) + if any(not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color) for color in colors): + raise ValueError('Title colors must use #RRGGBB format.') + return colors + + +def _title_image( + title: str, + max_width: int, + font_path: str = TITLE_FONT_PATH, + font_size: int = 100, + first_line_color: str = '#ffffff', + other_lines_color: str = '#ffe735', + effects: bool = True, + max_height: int = 360, + uppercase: bool = True, +): + """Lay out a centered, wrapped title with safe margins and a layered shadow.""" + import numpy as np + from PIL import Image, ImageColor, ImageDraw, ImageFilter, ImageFont + + if uppercase: + title = title.upper() + padding = 24 + outline = 6 if effects else 1 + text_width = max_width - padding * 2 + measure = ImageDraw.Draw(Image.new("RGBA", (1, 1))) + # Shrink long titles to keep the map visible. Character wrapping also handles + # URLs and words that are wider than the available space. + for fitted_size in range(font_size, 19, -4): + font = ImageFont.truetype(font_path, fitted_size) + lines = [] + for paragraph in title.splitlines() or [title]: + line = "" + for word in paragraph.split(): + candidate = f"{line} {word}".strip() + if measure.textlength(candidate, font=font) <= text_width: + line = candidate + continue + if line: + lines.append(line) + line = "" + for char in word: + if line and measure.textlength(line + char, font=font) > text_width: + lines.append(line) + line = "" + line += char + lines.append(line) + text = "\n".join(lines) + spacing = round(fitted_size * 0.22) + bounds = measure.multiline_textbbox((0, 0), text, font=font, + spacing=spacing, align="center", stroke_width=outline) + height = bounds[3] - bounds[1] + if height <= max_height: + break + if height > max_height: + raise ValueError("Video title is too long. Please shorten it.") + + size = (max_width, height + padding * 2) + shadow = Image.new("RGBA", size) + # Account for the font's vertical bounding-box offset inside the padded image. + xy = (max_width / 2, padding - bounds[1]) + # Use one line advance for shadow and colored text. Different stroke widths + # in multiline_text would otherwise change the spacing and clip lower rows. + advance = measure.textbbox((0, 0), 'A', font=font, stroke_width=outline)[3] + outline + spacing + shadow_draw = ImageDraw.Draw(shadow) + for row, line in enumerate(lines): + shadow_draw.text((xy[0], xy[1] + row * advance + 5), line, + font=font, anchor='ma', fill=(0, 0, 0, 255), stroke_width=outline) + # Combine a soft halo with a tighter drop shadow for contrast on bright footage. + # Preview and final video both use this image, so the shading stays consistent. + image = shadow.filter(ImageFilter.GaussianBlur(8)) if effects else Image.new('RGBA', size) + if effects: + image = Image.alpha_composite(image, shadow.filter(ImageFilter.GaussianBlur(2))) + draw = ImageDraw.Draw(image) + first_rgb = ImageColor.getrgb(first_line_color) + other_rgb = ImageColor.getrgb(other_lines_color) + for row, line in enumerate(lines): + color = first_rgb if row == 0 else other_rgb + draw.text((xy[0], xy[1] + row * advance), line, font=font, anchor='ma', + fill=color + (255,), stroke_width=outline, + stroke_fill=(0, 0, 0, 255) if effects else color + (220,)) + return np.array(image) + + +def _build_title_clip( + title: str, + duration: float, + max_width: int, + font_path: str = TITLE_FONT_PATH, + font_size: int = 100, + first_line_color: str = '#ffffff', + other_lines_color: str = '#ffe735', + effects: bool = True, + uppercase: bool = True, +): + from moviepy import ImageClip + + return ImageClip( + _title_image( + title, + max_width, + font_path, + font_size, + first_line_color, + other_lines_color, + effects, + uppercase=uppercase, + ) + ).with_duration(duration) + + +def _watermark_settings(data): + text = data.get('watermark_text', '') + color = data.get('watermark_color', '#a4e9ee') + if not isinstance(text, str) or len(text) > 80: + raise ValueError('Watermark text must be at most 80 characters.') + if not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color): + raise ValueError('Watermark color must use #RRGGBB format.') + return text.strip(), color + + +def _watermark_font_path(data): + # Validate the watermark's own font against the bundled filenames. + return _title_settings({'title_font': data.get('watermark_font', 'DIN Condensed.ttf')})[0] + + +def _round_watermark(image, diameter=230): + """Center-crop the logo to a circle while preserving existing transparency.""" + from PIL import Image, ImageChops, ImageDraw, ImageOps + + image = ImageOps.fit(image.convert('RGBA'), (diameter, diameter), method=Image.Resampling.LANCZOS) + # Draw the mask at higher resolution for a smooth circular edge. + mask = Image.new('L', (diameter * 4, diameter * 4)) + ImageDraw.Draw(mask).ellipse((0, 0, mask.width - 1, mask.height - 1), fill=255) + mask = mask.resize(image.size, Image.Resampling.LANCZOS) + image.putalpha(ImageChops.multiply(image.getchannel('A'), mask)) + return image + + +def _with_shadow(image): + """Add a soft silhouette shadow while keeping transparent areas transparent.""" + from PIL import Image, ImageFilter + + image = image.convert('RGBA') + canvas = Image.new('RGBA', (image.width + 48, image.height + 48)) + alpha = Image.new('L', canvas.size) + alpha.paste(image.getchannel('A'), (24, 32)) + shadow = Image.new('RGBA', canvas.size, (0, 0, 0, 0)) + shadow.putalpha(alpha.filter(ImageFilter.GaussianBlur(10)).point(lambda p: round(p * .8))) + canvas = Image.alpha_composite(canvas, shadow) + canvas.alpha_composite(image, (24, 24)) + return canvas + diff --git a/yt-dlp_serv.py b/yt-dlp_serv.py index 37ba122..7eb00b6 100644 --- a/yt-dlp_serv.py +++ b/yt-dlp_serv.py @@ -408,910 +408,33 @@ def cut_audio() -> tuple[object, int]: "error": str(e) }), 500 -TITLE_FONT_PATH = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "resources", "fonts", "DIN Condensed.ttf", +from video_text import ( + TITLE_FONT_PATH, + _build_title_clip, + _round_watermark, + _title_colors, + _title_image, + _title_settings, + _watermark_font_path, + _watermark_settings, + _with_shadow, ) - - -def _title_settings(data): - font_name = str(data.get('title_font') or 'DIN Condensed.ttf') - font_dir = os.path.dirname(TITLE_FONT_PATH) - # Accept bundled filenames only, rather than arbitrary client-supplied paths. - fonts = {name for name in os.listdir(font_dir) if name.lower().endswith(('.ttf', '.otf'))} - if font_name not in fonts: - raise ValueError('Unknown title font.') - raw_size = data.get('title_font_size', 100) - # Reject booleans explicitly because int(True) would otherwise succeed. - if isinstance(raw_size, bool): - raise ValueError('Title size must be a number between 48 and 144.') - try: - size = int(raw_size) - except (TypeError, ValueError, OverflowError): - raise ValueError('Title size must be a number between 48 and 144.') - if not 48 <= size <= 144: - raise ValueError('Title size must be between 48 and 144.') - return os.path.join(font_dir, font_name), size - - -def _title_colors(data): - colors = (data.get('title_first_line_color', '#ffffff'), - data.get('title_other_lines_color', '#ffe735')) - if any(not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color) for color in colors): - raise ValueError('Title colors must use #RRGGBB format.') - return colors - - -def _title_image( - title: str, - max_width: int, - font_path: str = TITLE_FONT_PATH, - font_size: int = 100, - first_line_color: str = '#ffffff', - other_lines_color: str = '#ffe735', - effects: bool = True, - max_height: int = 360, - uppercase: bool = True, -): - """Lay out a centered, wrapped title with safe margins and a layered shadow.""" - import numpy as np - from PIL import Image, ImageColor, ImageDraw, ImageFilter, ImageFont - - if uppercase: - title = title.upper() - padding = 24 - outline = 6 if effects else 1 - text_width = max_width - padding * 2 - measure = ImageDraw.Draw(Image.new("RGBA", (1, 1))) - # Shrink long titles to keep the map visible. Character wrapping also handles - # URLs and words that are wider than the available space. - for fitted_size in range(font_size, 19, -4): - font = ImageFont.truetype(font_path, fitted_size) - lines = [] - for paragraph in title.splitlines() or [title]: - line = "" - for word in paragraph.split(): - candidate = f"{line} {word}".strip() - if measure.textlength(candidate, font=font) <= text_width: - line = candidate - continue - if line: - lines.append(line) - line = "" - for char in word: - if line and measure.textlength(line + char, font=font) > text_width: - lines.append(line) - line = "" - line += char - lines.append(line) - text = "\n".join(lines) - spacing = round(fitted_size * 0.22) - bounds = measure.multiline_textbbox((0, 0), text, font=font, - spacing=spacing, align="center", stroke_width=outline) - height = bounds[3] - bounds[1] - if height <= max_height: - break - if height > max_height: - raise ValueError("Video title is too long. Please shorten it.") - - size = (max_width, height + padding * 2) - shadow = Image.new("RGBA", size) - # Account for the font's vertical bounding-box offset inside the padded image. - xy = (max_width / 2, padding - bounds[1]) - # Use one line advance for shadow and colored text. Different stroke widths - # in multiline_text would otherwise change the spacing and clip lower rows. - advance = measure.textbbox((0, 0), 'A', font=font, stroke_width=outline)[3] + outline + spacing - shadow_draw = ImageDraw.Draw(shadow) - for row, line in enumerate(lines): - shadow_draw.text((xy[0], xy[1] + row * advance + 5), line, - font=font, anchor='ma', fill=(0, 0, 0, 255), stroke_width=outline) - # Combine a soft halo with a tighter drop shadow for contrast on bright footage. - # Preview and final video both use this image, so the shading stays consistent. - image = shadow.filter(ImageFilter.GaussianBlur(8)) if effects else Image.new('RGBA', size) - if effects: - image = Image.alpha_composite(image, shadow.filter(ImageFilter.GaussianBlur(2))) - draw = ImageDraw.Draw(image) - first_rgb = ImageColor.getrgb(first_line_color) - other_rgb = ImageColor.getrgb(other_lines_color) - for row, line in enumerate(lines): - color = first_rgb if row == 0 else other_rgb - draw.text((xy[0], xy[1] + row * advance), line, font=font, anchor='ma', - fill=color + (255,), stroke_width=outline, - stroke_fill=(0, 0, 0, 255) if effects else color + (220,)) - return np.array(image) - - -def _build_title_clip( - title: str, - duration: float, - max_width: int, - font_path: str = TITLE_FONT_PATH, - font_size: int = 100, - first_line_color: str = '#ffffff', - other_lines_color: str = '#ffe735', - effects: bool = True, - uppercase: bool = True, -): - from moviepy import ImageClip - - return ImageClip( - _title_image( - title, - max_width, - font_path, - font_size, - first_line_color, - other_lines_color, - effects, - uppercase=uppercase, - ) - ).with_duration(duration) - - -def _watermark_settings(data): - text = data.get('watermark_text', '') - color = data.get('watermark_color', '#a4e9ee') - if not isinstance(text, str) or len(text) > 80: - raise ValueError('Watermark text must be at most 80 characters.') - if not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color): - raise ValueError('Watermark color must use #RRGGBB format.') - return text.strip(), color - - -def _watermark_font_path(data): - # Validate the watermark's own font against the bundled filenames. - return _title_settings({'title_font': data.get('watermark_font', 'DIN Condensed.ttf')})[0] - - -def _round_watermark(image, diameter=230): - """Center-crop the logo to a circle while preserving existing transparency.""" - from PIL import Image, ImageChops, ImageDraw, ImageOps - - image = ImageOps.fit(image.convert('RGBA'), (diameter, diameter), method=Image.Resampling.LANCZOS) - # Draw the mask at higher resolution for a smooth circular edge. - mask = Image.new('L', (diameter * 4, diameter * 4)) - ImageDraw.Draw(mask).ellipse((0, 0, mask.width - 1, mask.height - 1), fill=255) - mask = mask.resize(image.size, Image.Resampling.LANCZOS) - image.putalpha(ImageChops.multiply(image.getchannel('A'), mask)) - return image - - -def _with_shadow(image): - """Add a soft silhouette shadow while keeping transparent areas transparent.""" - from PIL import Image, ImageFilter - - image = image.convert('RGBA') - canvas = Image.new('RGBA', (image.width + 48, image.height + 48)) - alpha = Image.new('L', canvas.size) - alpha.paste(image.getchannel('A'), (24, 32)) - shadow = Image.new('RGBA', canvas.size, (0, 0, 0, 0)) - shadow.putalpha(alpha.filter(ImageFilter.GaussianBlur(10)).point(lambda p: round(p * .8))) - canvas = Image.alpha_composite(canvas, shadow) - canvas.alpha_composite(image, (24, 24)) - return canvas - - -from sticker_themes import ( +from video_stickers import ( ALLOWED_STICKERS, TWEMOJI_CODEPOINTS, - AUTO_STICKER_THEMES as _AUTO_STICKER_THEMES, - EXACT_WORD_KEYWORDS as _EXACT_WORD_KEYWORDS, + _parse_sticker_spec, + _resolve_auto_stickers, + _stars_row_image, + _sticker_image, + _subscribe_badge_image, +) +from video_render import ( + _apply_clip_fade_in, + _flatten_static_layers, + _prepare_background, + _render_threads, + _x264_preset, ) - - -def _resolve_auto_stickers(title: str) -> tuple[str, str]: - """Choose a thematic (left, right) sticker pair based on English title keywords.""" - lowered = (title or '').lower() - if not lowered.strip(): - return ('globe', 'pin') - best_pair: tuple[str, str] | None = None - best_count = 0 - # Score each theme by distinct keyword hits; ties keep the earlier (higher-priority) theme. - for keywords, pair in _AUTO_STICKER_THEMES: - count = 0 - for kw in keywords: - if kw in _EXACT_WORD_KEYWORDS: - pattern = rf'(? best_count: - best_count = count - best_pair = pair - return best_pair if best_pair is not None else ('globe', 'pin') - - -def _star_polygon(cx: float, cy: float, outer_r: float, inner_r: float) -> list[tuple[float, float]]: - """Return the 10 vertices of a 5-pointed star centered at (cx, cy).""" - import math - - points: list[tuple[float, float]] = [] - for i in range(10): - angle = -math.pi / 2 + i * (math.pi / 5) - r = outer_r if i % 2 == 0 else inner_r - points.append((cx + r * math.cos(angle), cy + r * math.sin(angle))) - return points - - -def _draw_heart(draw, cx: float, cy: float, size: float, fill: tuple[int, int, int, int], - outline: tuple[int, int, int, int] = (0, 0, 0, 255), width: int = 4) -> None: - """Draw a smooth vector heart icon centered at (cx, cy).""" - import math - - pts: list[tuple[float, float]] = [] - for step in range(64): - t = (step / 64.0) * 2.0 * math.pi - x = 16.0 * (math.sin(t) ** 3) - y = -(13.0 * math.cos(t) - 5.0 * math.cos(2 * t) - 2.0 * math.cos(3 * t) - math.cos(4 * t)) - pts.append((cx + x * (size / 34.0), cy + y * (size / 34.0))) - draw.polygon(pts, fill=fill, outline=outline, width=width) - - -def _subscribe_badge_image( - text: str = 'Subscribe!', - font_path: str = TITLE_FONT_PATH, - effects: bool = True, -): - """Render the dark rounded '[ ❤️ Subscribe! ▶️ ]' call-to-action pill badge.""" - import numpy as np - from PIL import Image, ImageDraw, ImageFont - - label = (text or 'Subscribe!').strip()[:40] or 'Subscribe!' - scale = 2 - font_size = 52 * scale - font = ImageFont.truetype(font_path, font_size) - measure = ImageDraw.Draw(Image.new('RGBA', (1, 1))) - bbox = measure.textbbox((0, 0), label, font=font, stroke_width=3 * scale) - text_w = bbox[2] - bbox[0] - - heart_slot = 64 * scale - play_w, play_h = 68 * scale, 48 * scale - pad_x = 28 * scale - gap = 18 * scale - pill_h = 84 * scale - pill_w = pad_x * 2 + heart_slot + gap * 2 + text_w + play_w - - canvas = Image.new('RGBA', (pill_w + 16 * scale, pill_h + 16 * scale), (0, 0, 0, 0)) - draw = ImageDraw.Draw(canvas) - ox, oy = 8 * scale, 8 * scale - - # Rounded dark charcoal pill with subtle glossy border - draw.rounded_rectangle( - (ox, oy, ox + pill_w, oy + pill_h), - radius=26 * scale, - fill=(28, 30, 36, 228), - outline=(255, 255, 255, 58), - width=2 * scale, - ) - - # Left icon: glossy red heart - heart_cx = ox + pad_x + heart_slot / 2 - heart_cy = oy + pill_h / 2 + 2 * scale - _draw_heart( - draw, - heart_cx, - heart_cy, - 54 * scale, - fill=(255, 42, 75, 255), - outline=(15, 15, 20, 255), - width=3 * scale, - ) - # Specular highlight on top-left lobe of heart - draw.ellipse( - (heart_cx - 16 * scale, heart_cy - 16 * scale, heart_cx - 6 * scale, heart_cy - 7 * scale), - fill=(255, 190, 200, 190), - ) - - # Center text: bold white Subscribe! text with dark stroke - text_x = ox + pad_x + heart_slot + gap + text_w / 2 - text_y = oy + pill_h / 2 - draw.text( - (text_x, text_y), - label, - font=font, - anchor='mm', - fill=(255, 255, 255, 255), - stroke_width=3 * scale, - stroke_fill=(0, 0, 0, 240), - ) - - # Right icon: red YouTube-style rounded play button - play_x0 = ox + pad_x + heart_slot + gap * 2 + text_w - play_y0 = oy + (pill_h - play_h) / 2 - draw.rounded_rectangle( - (play_x0, play_y0, play_x0 + play_w, play_y0 + play_h), - radius=12 * scale, - fill=(255, 32, 32, 255), - outline=(15, 15, 20, 255), - width=3 * scale, - ) - tri_cx = play_x0 + play_w / 2 + 2 * scale - tri_cy = play_y0 + play_h / 2 - tri = [ - (tri_cx - 11 * scale, tri_cy - 12 * scale), - (tri_cx - 11 * scale, tri_cy + 12 * scale), - (tri_cx + 13 * scale, tri_cy), - ] - draw.polygon(tri, fill=(255, 255, 255, 255)) - - final_img = canvas.resize((canvas.width // scale, canvas.height // scale), Image.Resampling.LANCZOS) - if effects: - final_img = _with_shadow(final_img) - return np.array(final_img) - - -def _stars_row_image(color: str = '#ffffff', effects: bool = True): - """Render three 5-pointed stars ('★ ★ ★') for the bottom watermark footer.""" - import numpy as np - from PIL import Image, ImageColor, ImageDraw - - if not isinstance(color, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', color): - color = '#ffffff' - rgb = ImageColor.getrgb(color) - - scale = 2 - w, h = 320 * scale, 76 * scale - canvas = Image.new('RGBA', (w, h), (0, 0, 0, 0)) - draw = ImageDraw.Draw(canvas) - - centers_x = (w * 0.22, w * 0.50, w * 0.78) - cy = h * 0.52 - for cx in centers_x: - pts = _star_polygon(cx, cy, outer_r=26 * scale, inner_r=11 * scale) - draw.polygon( - pts, - fill=rgb + (255,), - outline=(0, 0, 0, 240) if effects else rgb + (220,), - width=4 * scale if effects else 2 * scale, - ) - - final_img = canvas.resize((w // scale, h // scale), Image.Resampling.LANCZOS) - if effects: - final_img = _with_shadow(final_img) - return np.array(final_img) - - -def _parse_sticker_spec(raw_kind: str, title: str = '', side: str = 'left') -> tuple[str | None, str]: - """Parse a sticker specification into ``(gen_style_or_none, base_kind)``.""" - raw = (raw_kind or 'none').strip().lower() - gen_style: str | None = None - if ':' in raw: - prefix, _, base_kind = raw.partition(':') - if prefix in ('gen', 'gen_diecut', 'gen_neon', 'gen_stamp'): - gen_style = 'diecut' if prefix == 'gen' else prefix.removeprefix('gen_') - raw = base_kind or 'auto' - if raw == 'auto': - left_auto, right_auto = _resolve_auto_stickers(title) - raw = right_auto if side == 'right' else left_auto - return gen_style, raw - - -def _style_generated_sticker(icon_canvas, gen_style: str, size: int): - """Wrap a procedural vector icon in a generated sticker style (die-cut, neon, or stamp).""" - from PIL import Image, ImageDraw, ImageFilter - - s = icon_canvas.width - if gen_style == 'diecut': - # Create a crisp white vinyl die-cut sticker border around the vector silhouette. - alpha = icon_canvas.getchannel('A') - dilate = max(3, int(round(s * 0.055))) | 1 - border_mask = alpha.filter(ImageFilter.MaxFilter(dilate)) - vinyl = Image.new('RGBA', (s, s), (255, 255, 255, 0)) - vinyl.putalpha(border_mask) - white_layer = Image.new('RGBA', (s, s), (252, 253, 255, 255)) - white_layer.putalpha(border_mask) - styled = Image.alpha_composite(white_layer, icon_canvas) - styled = styled.rotate(6, resample=Image.Resampling.BICUBIC, expand=False) - return styled.resize((size, size), Image.Resampling.LANCZOS) - - if gen_style == 'neon': - badge = Image.new('RGBA', (s, s), (0, 0, 0, 0)) - glow = Image.new('RGBA', (s, s), (0, 0, 0, 0)) - gdraw = ImageDraw.Draw(glow) - pad = int(s * 0.08) - gdraw.ellipse((pad, pad, s - pad, s - pad), outline=(56, 189, 248, 230), width=int(s * 0.055)) - glow = glow.filter(ImageFilter.GaussianBlur(max(4, s // 28))) - bdraw = ImageDraw.Draw(badge) - bdraw.ellipse((pad, pad, s - pad, s - pad), fill=(15, 23, 42, 238), outline=(99, 102, 241, 255), width=int(s * 0.032)) - bdraw.ellipse((pad + 6, pad + 6, s - pad - 6, s - pad - 6), outline=(56, 189, 248, 210), width=max(2, s // 70)) - badge = Image.alpha_composite(glow, badge) - inner_size = int(s * 0.68) - icon_small = icon_canvas.resize((inner_size, inner_size), Image.Resampling.LANCZOS) - offset = (s - inner_size) // 2 - badge.alpha_composite(icon_small, (offset, offset)) - return badge.resize((size, size), Image.Resampling.LANCZOS) - - # 'stamp': tilted editorial badge with double accent frame and corner sparkles - stamp = Image.new('RGBA', (s, s), (0, 0, 0, 0)) - sdraw = ImageDraw.Draw(stamp) - pad = int(s * 0.08) - sdraw.rounded_rectangle((pad, pad, s - pad, s - pad), radius=int(s * 0.22), - fill=(30, 27, 75, 235), outline=(251, 191, 36, 255), width=int(s * 0.03)) - sdraw.rounded_rectangle((pad + 10, pad + 10, s - pad - 10, s - pad - 10), radius=int(s * 0.17), - outline=(255, 255, 255, 110), width=max(2, s // 80)) - # Tiny decorative sparkle stars in top corners - for sx, sy in ((s * 0.22, s * 0.22), (s * 0.78, s * 0.22)): - sdraw.polygon(_star_polygon(sx, sy, s * 0.055, s * 0.022), fill=(253, 224, 71, 240)) - inner_size = int(s * 0.68) - icon_small = icon_canvas.resize((inner_size, inner_size), Image.Resampling.LANCZOS) - offset = (s - inner_size) // 2 - stamp.alpha_composite(icon_small, (offset, offset + int(s * 0.03))) - stamp = stamp.rotate(-6, resample=Image.Resampling.BICUBIC, expand=False) - return stamp.resize((size, size), Image.Resampling.LANCZOS) - - -from extra_stickers import draw_extra_sticker - - -def _draw_extra_sticker(kind, draw, s, outline, ow): - """Delegate extended procedural sticker drawing to extra_stickers.py.""" - return draw_extra_sticker(kind, draw, s, outline, ow, star_polygon_fn=_star_polygon) - - -def _sticker_image(kind: str, size: int = 140, effects: bool = True, allow_remote: bool = False): - """Render a decorative top-corner sticker (library Twemoji or procedural generated style).""" - import numpy as np - from PIL import Image, ImageDraw, ImageOps - - gen_style, kind = _parse_sticker_spec(kind) - if kind not in ALLOWED_STICKERS or kind == 'none': - return None - - # Library stickers (without a gen_ prefix) use Twemoji cache/CDN when available. - codepoint = None if gen_style else TWEMOJI_CODEPOINTS.get(kind) - if codepoint: - stickers_dir = os.path.join(STORAGE_DIR, 'stickers') - cache_path = os.path.join(stickers_dir, f'{kind}.png') - if not os.path.isfile(cache_path) and allow_remote: - try: - import requests - os.makedirs(stickers_dir, exist_ok=True) - url = f'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/72x72/{codepoint}.png' - resp = requests.get(url, timeout=1.5) - if resp.status_code == 200 and resp.content: - with Image.open(io.BytesIO(resp.content)) as fetched: - fetched.convert('RGBA').save(cache_path, format='PNG') - except Exception: - pass - if os.path.isfile(cache_path): - try: - with Image.open(cache_path) as cached: - final_img = ImageOps.contain(cached.convert('RGBA'), (size, size), method=Image.Resampling.LANCZOS) - if final_img.size != (size, size): - padded = Image.new('RGBA', (size, size), (0, 0, 0, 0)) - padded.paste(final_img, ((size - final_img.width) // 2, (size - final_img.height) // 2)) - final_img = padded - if effects: - final_img = _with_shadow(final_img) - return np.array(final_img) - except Exception: - pass - - scale = 2 - s = size * scale - canvas = Image.new('RGBA', (s, s), (0, 0, 0, 0)) - draw = ImageDraw.Draw(canvas) - outline = (18, 18, 24, 255) - ow = 4 * scale - - if _draw_extra_sticker(kind, draw, s, outline, ow): - pass - elif kind == 'santa': - # Fluffy white beard base - draw.ellipse((s * 0.16, s * 0.44, s * 0.84, s * 0.94), fill=(250, 252, 255, 255), outline=outline, width=ow) - draw.ellipse((s * 0.12, s * 0.52, s * 0.46, s * 0.84), fill=(250, 252, 255, 255), outline=outline, width=ow) - draw.ellipse((s * 0.54, s * 0.52, s * 0.88, s * 0.84), fill=(250, 252, 255, 255), outline=outline, width=ow) - # Warm face - draw.ellipse((s * 0.24, s * 0.30, s * 0.76, s * 0.72), fill=(255, 214, 186, 255), outline=outline, width=ow) - # Re-fill inner beard over lower face - draw.chord((s * 0.18, s * 0.42, s * 0.82, s * 0.92), start=0, end=180, fill=(250, 252, 255, 255), outline=outline, width=ow) - # Rosy cheeks & eyes - draw.ellipse((s * 0.28, s * 0.48, s * 0.39, s * 0.56), fill=(255, 138, 138, 210)) - draw.ellipse((s * 0.61, s * 0.48, s * 0.72, s * 0.56), fill=(255, 138, 138, 210)) - draw.ellipse((s * 0.35, s * 0.41, s * 0.42, s * 0.48), fill=outline) - draw.ellipse((s * 0.58, s * 0.41, s * 0.65, s * 0.48), fill=outline) - # Mustache & rosy nose - draw.ellipse((s * 0.30, s * 0.53, s * 0.52, s * 0.65), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.48, s * 0.53, s * 0.70, s * 0.65), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.44, s * 0.47, s * 0.56, s * 0.57), fill=(244, 92, 92, 255), outline=outline, width=ow // 2) - # Red Santa hat & white fur brim + pom-pom - hat_pts = [(s * 0.18, s * 0.34), (s * 0.52, s * 0.06), (s * 0.86, s * 0.28), (s * 0.80, s * 0.36)] - draw.polygon(hat_pts, fill=(228, 34, 46, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.15, s * 0.27, s * 0.83, s * 0.40), radius=14 * scale, fill=(252, 253, 255, 255), outline=outline, width=ow) - draw.ellipse((s * 0.76, s * 0.18, s * 0.94, s * 0.36), fill=(252, 253, 255, 255), outline=outline, width=ow) - - elif kind == 'gift': - # Candy cane behind top-right of gift box - draw.arc((s * 0.52, s * 0.06, s * 0.90, s * 0.46), start=180, end=20, fill=(235, 38, 50, 255), width=12 * scale) - # Golden ribbon bow Loops on top - draw.ellipse((s * 0.24, s * 0.14, s * 0.52, s * 0.38), fill=(255, 204, 42, 255), outline=outline, width=ow) - draw.ellipse((s * 0.48, s * 0.14, s * 0.76, s * 0.38), fill=(255, 204, 42, 255), outline=outline, width=ow) - # Red gift box body - draw.rounded_rectangle((s * 0.18, s * 0.36, s * 0.82, s * 0.90), radius=12 * scale, fill=(224, 36, 48, 255), outline=outline, width=ow) - # Gift box lid - draw.rounded_rectangle((s * 0.14, s * 0.28, s * 0.86, s * 0.44), radius=10 * scale, fill=(242, 52, 64, 255), outline=outline, width=ow) - # Vertical & horizontal gold ribbon stripes - draw.rectangle((s * 0.44, s * 0.28, s * 0.56, s * 0.90), fill=(255, 208, 46, 255), outline=outline, width=ow // 2) - draw.rectangle((s * 0.18, s * 0.58, s * 0.82, s * 0.69), fill=(255, 208, 46, 255), outline=outline, width=ow // 2) - # Holly leaves & berry accent - draw.ellipse((s * 0.64, s * 0.20, s * 0.88, s * 0.34), fill=(40, 184, 86, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.44, s * 0.22, s * 0.56, s * 0.34), fill=(255, 230, 90, 255), outline=outline, width=ow // 2) - - elif kind == 'snowflake': - import math - draw.ellipse((s * 0.14, s * 0.14, s * 0.86, s * 0.86), fill=(48, 168, 255, 255), outline=outline, width=ow) - cx, cy = s * 0.50, s * 0.50 - for i in range(6): - ang = i * (math.pi / 3) - x2 = cx + s * 0.30 * math.cos(ang) - y2 = cy + s * 0.30 * math.sin(ang) - draw.line((cx, cy, x2, y2), fill=(255, 255, 255, 255), width=6 * scale) - bx = cx + s * 0.19 * math.cos(ang) - by = cy + s * 0.19 * math.sin(ang) - for delta in (-0.55, 0.55): - tx = bx + s * 0.09 * math.cos(ang + delta) - ty = by + s * 0.09 * math.sin(ang + delta) - draw.line((bx, by, tx, ty), fill=(255, 255, 255, 255), width=4 * scale) - draw.ellipse((s * 0.42, s * 0.42, s * 0.58, s * 0.58), fill=(225, 248, 255, 255), outline=outline, width=ow // 2) - - elif kind == 'pumpkin': - # Stem - draw.polygon([(s * 0.45, s * 0.10), (s * 0.57, s * 0.10), (s * 0.54, s * 0.28), (s * 0.46, s * 0.28)], - fill=(46, 164, 72, 255), outline=outline, width=ow) - # Ribbed orange pumpkin lobes - draw.ellipse((s * 0.12, s * 0.24, s * 0.58, s * 0.88), fill=(242, 116, 28, 255), outline=outline, width=ow) - draw.ellipse((s * 0.42, s * 0.24, s * 0.88, s * 0.88), fill=(242, 116, 28, 255), outline=outline, width=ow) - draw.ellipse((s * 0.26, s * 0.22, s * 0.74, s * 0.90), fill=(255, 138, 36, 255), outline=outline, width=ow) - # Glowing jack-o'-lantern eyes & grin - draw.polygon([(s * 0.30, s * 0.48), (s * 0.40, s * 0.36), (s * 0.45, s * 0.48)], fill=(255, 232, 64, 255), outline=outline, width=ow // 2) - draw.polygon([(s * 0.55, s * 0.48), (s * 0.60, s * 0.36), (s * 0.70, s * 0.48)], fill=(255, 232, 64, 255), outline=outline, width=ow // 2) - draw.polygon([(s * 0.28, s * 0.64), (s * 0.50, s * 0.72), (s * 0.72, s * 0.64), (s * 0.64, s * 0.78), (s * 0.36, s * 0.78)], - fill=(255, 232, 64, 255), outline=outline, width=ow // 2) - - elif kind == 'globe': - draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(36, 146, 245, 255), outline=outline, width=ow) - # Stylized green continents - draw.polygon([(s * 0.26, s * 0.30), (s * 0.46, s * 0.24), (s * 0.48, s * 0.46), (s * 0.34, s * 0.66), (s * 0.22, s * 0.50)], fill=(58, 204, 112, 255), outline=outline, width=ow // 2) - draw.polygon([(s * 0.56, s * 0.28), (s * 0.78, s * 0.36), (s * 0.74, s * 0.68), (s * 0.54, s * 0.58)], fill=(58, 204, 112, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), outline=outline, width=ow) - - elif kind == 'compass': - draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(245, 194, 48, 255), outline=outline, width=ow) - draw.ellipse((s * 0.20, s * 0.20, s * 0.80, s * 0.80), fill=(30, 42, 62, 255), outline=outline, width=ow // 2) - # North red pointer & South white pointer - draw.polygon([(s * 0.50, s * 0.24), (s * 0.60, s * 0.50), (s * 0.40, s * 0.50)], fill=(244, 54, 68, 255), outline=outline, width=ow // 2) - draw.polygon([(s * 0.50, s * 0.76), (s * 0.60, s * 0.50), (s * 0.40, s * 0.50)], fill=(245, 248, 255, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.45, s * 0.45, s * 0.55, s * 0.55), fill=(255, 214, 64, 255), outline=outline, width=ow // 2) - - elif kind == 'pin': - draw.polygon([(s * 0.24, s * 0.46), (s * 0.50, s * 0.90), (s * 0.76, s * 0.46)], fill=(238, 42, 62, 255), outline=outline, width=ow) - draw.ellipse((s * 0.20, s * 0.12, s * 0.80, s * 0.68), fill=(238, 42, 62, 255), outline=outline, width=ow) - draw.ellipse((s * 0.36, s * 0.26, s * 0.64, s * 0.54), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) - - elif kind == 'plane': - # Sky badge + white/blue jet plane - draw.ellipse((s * 0.14, s * 0.14, s * 0.86, s * 0.86), fill=(46, 156, 255, 255), outline=outline, width=ow) - wings = [(s * 0.18, s * 0.56), (s * 0.82, s * 0.24), (s * 0.64, s * 0.82), (s * 0.48, s * 0.58), (s * 0.32, s * 0.68)] - draw.polygon(wings, fill=(250, 252, 255, 255), outline=outline, width=ow) - - elif kind == 'money': - # Tie top + plump golden money sack - draw.polygon([(s * 0.36, s * 0.14), (s * 0.64, s * 0.14), (s * 0.58, s * 0.30), (s * 0.42, s * 0.30)], - fill=(238, 188, 52, 255), outline=outline, width=ow) - draw.ellipse((s * 0.18, s * 0.26, s * 0.82, s * 0.90), fill=(246, 198, 58, 255), outline=outline, width=ow) - draw.ellipse((s * 0.34, s * 0.44, s * 0.66, s * 0.76), fill=(46, 192, 96, 255), outline=outline, width=ow // 2) - - elif kind == 'chart': - draw.rounded_rectangle((s * 0.12, s * 0.14, s * 0.88, s * 0.88), radius=14 * scale, fill=(245, 248, 255, 255), outline=outline, width=ow) - draw.rectangle((s * 0.22, s * 0.56, s * 0.36, s * 0.80), fill=(52, 152, 245, 255), outline=outline, width=ow // 2) - draw.rectangle((s * 0.43, s * 0.40, s * 0.57, s * 0.80), fill=(46, 204, 113, 255), outline=outline, width=ow // 2) - draw.rectangle((s * 0.64, s * 0.24, s * 0.78, s * 0.80), fill=(255, 196, 42, 255), outline=outline, width=ow // 2) - - elif kind == 'diamond': - gem = [(s * 0.28, s * 0.18), (s * 0.72, s * 0.18), (s * 0.90, s * 0.40), (s * 0.50, s * 0.88), (s * 0.10, s * 0.40)] - draw.polygon(gem, fill=(56, 214, 255, 255), outline=outline, width=ow) - draw.polygon([(s * 0.28, s * 0.18), (s * 0.72, s * 0.18), (s * 0.64, s * 0.40), (s * 0.36, s * 0.40)], - fill=(165, 242, 255, 255), outline=outline, width=ow // 2) - - elif kind == 'shield': - pts = [(s * 0.18, s * 0.14), (s * 0.82, s * 0.14), (s * 0.82, s * 0.50), (s * 0.50, s * 0.90), (s * 0.18, s * 0.50)] - draw.polygon(pts, fill=(42, 118, 238, 255), outline=outline, width=ow) - inner = [(s * 0.28, s * 0.22), (s * 0.72, s * 0.22), (s * 0.72, s * 0.48), (s * 0.50, s * 0.78), (s * 0.28, s * 0.48)] - draw.polygon(inner, fill=(255, 204, 46, 255), outline=outline, width=ow // 2) - - elif kind == 'swords': - # Crossed silver blades with gold guards - draw.polygon([(s * 0.16, s * 0.22), (s * 0.24, s * 0.14), (s * 0.84, s * 0.74), (s * 0.76, s * 0.82)], - fill=(224, 232, 245, 255), outline=outline, width=ow) - draw.polygon([(s * 0.84, s * 0.22), (s * 0.76, s * 0.14), (s * 0.16, s * 0.74), (s * 0.24, s * 0.82)], - fill=(224, 232, 245, 255), outline=outline, width=ow) - draw.ellipse((s * 0.40, s * 0.40, s * 0.60, s * 0.60), fill=(255, 204, 46, 255), outline=outline, width=ow) - - elif kind == 'rocket': - # Exhaust flame - draw.polygon([(s * 0.38, s * 0.72), (s * 0.50, s * 0.94), (s * 0.62, s * 0.72)], fill=(255, 112, 34, 255), outline=outline, width=ow // 2) - # Side fins - draw.polygon([(s * 0.20, s * 0.74), (s * 0.34, s * 0.52), (s * 0.36, s * 0.74)], fill=(235, 44, 58, 255), outline=outline, width=ow) - draw.polygon([(s * 0.80, s * 0.74), (s * 0.66, s * 0.52), (s * 0.64, s * 0.74)], fill=(235, 44, 58, 255), outline=outline, width=ow) - # Rocket fuselage & nose cone - draw.polygon([(s * 0.50, s * 0.08), (s * 0.68, s * 0.34), (s * 0.66, s * 0.74), (s * 0.34, s * 0.74), (s * 0.32, s * 0.34)], - fill=(248, 250, 255, 255), outline=outline, width=ow) - draw.polygon([(s * 0.50, s * 0.08), (s * 0.66, s * 0.30), (s * 0.34, s * 0.30)], - fill=(235, 44, 58, 255), outline=outline, width=ow) - draw.ellipse((s * 0.40, s * 0.38, s * 0.60, s * 0.58), fill=(58, 198, 255, 255), outline=outline, width=ow // 2) - - elif kind == 'bolt': - bolt_pts = [ - (s * 0.56, s * 0.08), - (s * 0.22, s * 0.54), - (s * 0.48, s * 0.54), - (s * 0.38, s * 0.92), - (s * 0.78, s * 0.44), - (s * 0.52, s * 0.44), - ] - draw.polygon(bolt_pts, fill=(255, 214, 36, 255), outline=outline, width=ow) - - elif kind == 'fire': - outer_flame = [(s * 0.50, s * 0.08), (s * 0.82, s * 0.48), (s * 0.74, s * 0.84), (s * 0.50, s * 0.92), (s * 0.26, s * 0.84), (s * 0.18, s * 0.48)] - inner_flame = [(s * 0.50, s * 0.32), (s * 0.68, s * 0.58), (s * 0.62, s * 0.82), (s * 0.50, s * 0.88), (s * 0.38, s * 0.82), (s * 0.32, s * 0.58)] - draw.polygon(outer_flame, fill=(255, 86, 34, 255), outline=outline, width=ow) - draw.polygon(inner_flame, fill=(255, 214, 46, 255), outline=outline, width=ow // 2) - - elif kind == 'trophy': - # Handles - draw.ellipse((s * 0.12, s * 0.22, s * 0.40, s * 0.52), fill=(255, 204, 38, 255), outline=outline, width=ow) - draw.ellipse((s * 0.60, s * 0.22, s * 0.88, s * 0.52), fill=(255, 204, 38, 255), outline=outline, width=ow) - # Cup body + stem + base - draw.polygon([(s * 0.24, s * 0.16), (s * 0.76, s * 0.16), (s * 0.66, s * 0.58), (s * 0.34, s * 0.58)], - fill=(255, 214, 46, 255), outline=outline, width=ow) - draw.rectangle((s * 0.44, s * 0.56, s * 0.56, s * 0.76), fill=(235, 180, 28, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.28, s * 0.74, s * 0.72, s * 0.88), radius=8 * scale, fill=(215, 152, 24, 255), outline=outline, width=ow) - - elif kind == 'crown': - crown_pts = [ - (s * 0.14, s * 0.78), - (s * 0.10, s * 0.28), - (s * 0.34, s * 0.52), - (s * 0.50, s * 0.18), - (s * 0.66, s * 0.52), - (s * 0.90, s * 0.28), - (s * 0.86, s * 0.78), - ] - draw.polygon(crown_pts, fill=(255, 204, 38, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.14, s * 0.74, s * 0.86, s * 0.88), radius=8 * scale, fill=(235, 168, 20, 255), outline=outline, width=ow) - draw.ellipse((s * 0.45, s * 0.56, s * 0.55, s * 0.66), fill=(244, 42, 75, 255), outline=outline, width=ow // 2) - - elif kind == 'star': - pts = _star_polygon(s * 0.50, s * 0.52, outer_r=s * 0.40, inner_r=s * 0.18) - draw.polygon(pts, fill=(255, 216, 46, 255), outline=outline, width=ow) - - elif kind == 'heart': - _draw_heart(draw, s * 0.50, s * 0.52, s * 0.78, fill=(255, 42, 78, 255), outline=outline, width=ow) - - elif kind == 'skull': - draw.rounded_rectangle((s * 0.32, s * 0.56, s * 0.68, s * 0.88), radius=10 * scale, fill=(245, 247, 252, 255), outline=outline, width=ow) - draw.ellipse((s * 0.18, s * 0.14, s * 0.82, s * 0.74), fill=(245, 247, 252, 255), outline=outline, width=ow) - draw.ellipse((s * 0.28, s * 0.36, s * 0.44, s * 0.52), fill=outline) - draw.ellipse((s * 0.56, s * 0.36, s * 0.72, s * 0.52), fill=outline) - draw.polygon([(s * 0.50, s * 0.50), (s * 0.45, s * 0.60), (s * 0.55, s * 0.60)], fill=outline) - - elif kind == 'bear': - # Round ears + warm inner ears - draw.ellipse((s * 0.12, s * 0.12, s * 0.42, s * 0.42), fill=(142, 82, 42, 255), outline=outline, width=ow) - draw.ellipse((s * 0.58, s * 0.12, s * 0.88, s * 0.42), fill=(142, 82, 42, 255), outline=outline, width=ow) - draw.ellipse((s * 0.19, s * 0.19, s * 0.35, s * 0.35), fill=(216, 158, 108, 255)) - draw.ellipse((s * 0.65, s * 0.19, s * 0.81, s * 0.35), fill=(216, 158, 108, 255)) - # Plump brown bear head - draw.ellipse((s * 0.16, s * 0.22, s * 0.84, s * 0.88), fill=(158, 94, 48, 255), outline=outline, width=ow) - # Light tan muzzle + dark nose - draw.ellipse((s * 0.32, s * 0.48, s * 0.68, s * 0.78), fill=(234, 192, 146, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.42, s * 0.50, s * 0.58, s * 0.62), fill=outline) - # Eyes - draw.ellipse((s * 0.31, s * 0.38, s * 0.39, s * 0.46), fill=outline) - draw.ellipse((s * 0.61, s * 0.38, s * 0.69, s * 0.46), fill=outline) - - elif kind == 'paw': - draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(252, 211, 77, 255), outline=outline, width=ow) - pad_color = (120, 53, 15, 255) - draw.ellipse((s * 0.30, s * 0.48, s * 0.70, s * 0.78), fill=pad_color, outline=outline, width=ow // 2) - for px, py in ((0.24, 0.34), (0.39, 0.24), (0.61, 0.24), (0.76, 0.34)): - draw.ellipse((s * (px - 0.08), s * (py - 0.09), s * (px + 0.08), s * (py + 0.09)), fill=pad_color, outline=outline, width=ow // 2) - - elif kind == 'tree': - draw.rectangle((s * 0.42, s * 0.68, s * 0.58, s * 0.90), fill=(132, 78, 42, 255), outline=outline, width=ow) - draw.polygon([(s * 0.50, s * 0.34), (s * 0.86, s * 0.74), (s * 0.14, s * 0.74)], fill=(22, 163, 74, 255), outline=outline, width=ow) - draw.polygon([(s * 0.50, s * 0.20), (s * 0.80, s * 0.56), (s * 0.20, s * 0.56)], fill=(34, 197, 94, 255), outline=outline, width=ow) - draw.polygon([(s * 0.50, s * 0.08), (s * 0.72, s * 0.38), (s * 0.28, s * 0.38)], fill=(74, 222, 128, 255), outline=outline, width=ow) - - elif kind == 'wolf': - draw.polygon([(s * 0.18, s * 0.42), (s * 0.22, s * 0.10), (s * 0.42, s * 0.28)], fill=(100, 116, 139, 255), outline=outline, width=ow) - draw.polygon([(s * 0.82, s * 0.42), (s * 0.78, s * 0.10), (s * 0.58, s * 0.28)], fill=(100, 116, 139, 255), outline=outline, width=ow) - draw.polygon([(s * 0.16, s * 0.38), (s * 0.50, s * 0.22), (s * 0.84, s * 0.38), (s * 0.76, s * 0.68), (s * 0.50, s * 0.88), (s * 0.24, s * 0.68)], - fill=(148, 163, 184, 255), outline=outline, width=ow) - draw.polygon([(s * 0.34, s * 0.54), (s * 0.50, s * 0.88), (s * 0.66, s * 0.54)], fill=(241, 245, 249, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.32, s * 0.42, s * 0.42, s * 0.50), fill=(250, 204, 21, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.58, s * 0.42, s * 0.68, s * 0.50), fill=(250, 204, 21, 255), outline=outline, width=ow // 2) - draw.ellipse((s * 0.44, s * 0.74, s * 0.56, s * 0.84), fill=outline) - - elif kind == 'beer': - draw.rounded_rectangle((s * 0.62, s * 0.38, s * 0.86, s * 0.76), radius=12 * scale, outline=outline, width=ow) - draw.rounded_rectangle((s * 0.22, s * 0.28, s * 0.70, s * 0.88), radius=10 * scale, fill=(245, 158, 11, 255), outline=outline, width=ow) - for fx in (0.28, 0.44, 0.60): - draw.ellipse((s * (fx - 0.12), s * 0.14, s * (fx + 0.12), s * 0.38), fill=(255, 255, 255, 255), outline=outline, width=ow // 2) - - elif kind == 'coffee': - draw.rounded_rectangle((s * 0.64, s * 0.42, s * 0.86, s * 0.70), radius=12 * scale, outline=outline, width=ow) - draw.chord((s * 0.18, s * 0.28, s * 0.74, s * 0.84), start=0, end=180, fill=(248, 250, 252, 255), outline=outline, width=ow) - draw.ellipse((s * 0.18, s * 0.32, s * 0.74, s * 0.48), fill=(120, 53, 15, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.12, s * 0.78, s * 0.82, s * 0.88), radius=6 * scale, fill=(226, 232, 240, 255), outline=outline, width=ow) - - elif kind == 'wine': - draw.polygon([(s * 0.28, s * 0.14), (s * 0.72, s * 0.14), (s * 0.64, s * 0.54), (s * 0.36, s * 0.54)], fill=(225, 29, 72, 255), outline=outline, width=ow) - draw.rectangle((s * 0.47, s * 0.54, s * 0.53, s * 0.82), fill=(226, 232, 240, 255), outline=outline, width=ow // 2) - draw.rounded_rectangle((s * 0.32, s * 0.80, s * 0.68, s * 0.88), radius=6 * scale, fill=(226, 232, 240, 255), outline=outline, width=ow) - - elif kind == 'pizza': - draw.polygon([(s * 0.50, s * 0.88), (s * 0.16, s * 0.22), (s * 0.84, s * 0.22)], fill=(250, 204, 21, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.14, s * 0.14, s * 0.86, s * 0.28), radius=10 * scale, fill=(217, 119, 6, 255), outline=outline, width=ow) - for px, py in ((0.40, 0.40), (0.58, 0.44), (0.48, 0.62)): - draw.ellipse((s * (px - 0.07), s * (py - 0.07), s * (px + 0.07), s * (py + 0.07)), fill=(220, 38, 38, 255), outline=outline, width=ow // 2) - - elif kind == 'soccer': - draw.ellipse((s * 0.12, s * 0.12, s * 0.88, s * 0.88), fill=(248, 250, 252, 255), outline=outline, width=ow) - pent = _star_polygon(s * 0.50, s * 0.50, outer_r=s * 0.18, inner_r=s * 0.15) - draw.polygon(pent, fill=outline) - for vx, vy in pent[::2]: - draw.line((vx, vy, s * 0.50 + (vx - s * 0.50) * 2.0, s * 0.50 + (vy - s * 0.50) * 2.0), fill=outline, width=ow // 2) - - elif kind == 'car': - draw.polygon([(s * 0.24, s * 0.48), (s * 0.36, s * 0.28), (s * 0.66, s * 0.28), (s * 0.78, s * 0.48)], fill=(56, 189, 248, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.12, s * 0.46, s * 0.88, s * 0.74), radius=12 * scale, fill=(239, 68, 68, 255), outline=outline, width=ow) - draw.ellipse((s * 0.20, s * 0.62, s * 0.38, s * 0.80), fill=(30, 41, 59, 255), outline=outline, width=ow) - draw.ellipse((s * 0.62, s * 0.62, s * 0.80, s * 0.80), fill=(30, 41, 59, 255), outline=outline, width=ow) - - elif kind == 'brain': - for bx in (0.36, 0.64): - draw.ellipse((s * (bx - 0.22), s * 0.18, s * (bx + 0.22), s * 0.82), fill=(244, 114, 182, 255), outline=outline, width=ow) - draw.line((s * 0.50, s * 0.18, s * 0.50, s * 0.82), fill=outline, width=ow) - - elif kind == 'book': - draw.rounded_rectangle((s * 0.18, s * 0.58, s * 0.84, s * 0.82), radius=8 * scale, fill=(59, 130, 246, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.16, s * 0.38, s * 0.82, s * 0.62), radius=8 * scale, fill=(16, 185, 129, 255), outline=outline, width=ow) - draw.rounded_rectangle((s * 0.20, s * 0.18, s * 0.86, s * 0.42), radius=8 * scale, fill=(244, 63, 94, 255), outline=outline, width=ow) - - elif kind == 'sun': - pts = _star_polygon(s * 0.50, s * 0.50, outer_r=s * 0.42, inner_r=s * 0.28) - draw.polygon(pts, fill=(251, 146, 60, 255), outline=outline, width=ow) - draw.ellipse((s * 0.24, s * 0.24, s * 0.76, s * 0.76), fill=(250, 204, 21, 255), outline=outline, width=ow) - - if gen_style: - final_img = _style_generated_sticker(canvas, gen_style, size) - else: - final_img = canvas.resize((size, size), Image.Resampling.LANCZOS) - if effects: - final_img = _with_shadow(final_img) - return np.array(final_img) - - -def _apply_clip_fade_in(clip, total_duration: float, fade_start: float = 0.0, fade_duration: float = 0.6): - """Smoothly fade a clip's alpha mask in starting at ``fade_start`` over ``fade_duration`` seconds.""" - import numpy as np - from moviepy import VideoClip - - if fade_start <= 0.0 and fade_duration <= 0.0: - return clip.with_duration(total_duration) - base_frame = clip.get_frame(0) - if base_frame.ndim == 3 and base_frame.shape[2] == 4: - base_mask = base_frame[:, :, 3].astype(np.float32) / 255.0 - elif clip.mask is not None: - base_mask = clip.mask.get_frame(0).astype(np.float32) - else: - base_mask = np.ones((clip.h, clip.w), dtype=np.float32) - - def _mask_frame(t: float) -> np.ndarray: - if t < fade_start: - return np.zeros_like(base_mask) - if fade_duration <= 0.0 or t >= fade_start + fade_duration: - return base_mask - alpha = float((t - fade_start) / fade_duration) - return base_mask * max(0.0, min(1.0, alpha)) - - mask_clip = VideoClip(_mask_frame, is_mask=True, duration=total_duration) - return clip.with_duration(total_duration).with_mask(mask_clip) - - -def _x264_preset() -> str: - """x264 speed preset; ``VIDEO_X264_PRESET`` overrides the fast default.""" - allowed = ('ultrafast', 'superfast', 'veryfast', 'faster', 'fast', - 'medium', 'slow', 'slower', 'veryslow') - preset = os.environ.get('VIDEO_X264_PRESET', 'veryfast').strip().lower() - return preset if preset in allowed else 'veryfast' - - -def _render_threads() -> int: - """Encoder thread count; ``VIDEO_RENDER_THREADS`` overrides the CPU count.""" - try: - threads = int(os.environ.get('VIDEO_RENDER_THREADS', '0')) - except ValueError: - threads = 0 - return threads if threads > 0 else (os.cpu_count() or 2) - - -def _prepare_background(src: str, dst: str, duration: float, size=(1080, 1920), - effects: bool = True) -> None: - """Trim, cover-scale, center-crop and optionally soften the background in FFmpeg. - - This replaces per-frame MoviePy resizing and the Pillow blur (downscale to - a quarter, Gaussian blur radius 2, upscale) with one native pass. The - intermediate is encoded near-losslessly so the final encode sees the same - pixels the Python path produced. - """ - from moviepy.config import FFMPEG_BINARY - - w, h = size - filters = [f'scale={w}:{h}:force_original_aspect_ratio=increase:flags=lanczos', - f'crop={w}:{h}'] - if effects: - filters += [f'scale={w // 4}:{h // 4}:flags=bicubic', 'gblur=sigma=2', - f'scale={w}:{h}:flags=bicubic'] - filters.append('format=yuv420p') - command = [FFMPEG_BINARY, '-hide_banner', '-loglevel', 'error', '-nostdin', '-y', - '-i', src, '-t', f'{duration:.3f}', '-an', '-vf', ','.join(filters), - '-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '10', - '-threads', str(_render_threads()), dst] - result = subprocess.run(command, capture_output=True, text=True, timeout=600) - if result.returncode != 0: - raise RuntimeError(f'Background preprocessing failed: {result.stderr.strip()[-300:]}') - - -def _flatten_static_layers(clips, size, dynamic, duration): - """Merge consecutive static layers into single RGBA foreground clips. - - ``dynamic`` holds ids of clips that change over time (background, fades, - veil). Static runs between them are alpha-composited once with Pillow in - the same order and with the same positioning rules MoviePy uses, so each - output frame blends one foreground instead of many layers. - """ - import numpy as np - from PIL import Image - from moviepy import ImageClip - from moviepy.tools import compute_position - - out, canvas = [], None - - def flush(): - nonlocal canvas - if canvas is not None: - out.append(ImageClip(np.array(canvas)).with_duration(duration).with_position((0, 0))) - canvas = None - - for clip in clips: - if id(clip) in dynamic: - flush() - out.append(clip) - continue - frame = np.asarray(clip.get_frame(0)) - if frame.dtype != np.uint8: - frame = np.clip(frame, 0, 255).astype(np.uint8) - layer = Image.fromarray(frame[:, :, :3]).convert('RGBA') - if clip.mask is not None: - alpha = np.clip(np.asarray(clip.mask.get_frame(0)) * 255, 0, 255).astype(np.uint8) - layer.putalpha(Image.fromarray(alpha)) - elif frame.ndim == 3 and frame.shape[2] == 4: - layer.putalpha(Image.fromarray(frame[:, :, 3])) - pos = compute_position(layer.size, size, clip.pos(0), clip.relative_pos) - if canvas is None: - canvas = Image.new('RGBA', size, (0, 0, 0, 0)) - placed = Image.new('RGBA', size, (0, 0, 0, 0)) - placed.paste(layer, tuple(int(v) for v in pos)) - canvas = Image.alpha_composite(canvas, placed) - flush() - return out @app.route('/api/video_title_preview', methods=['POST']) From f9e199a1d9a35130196660bfb6fcda7bb71122d2 Mon Sep 17 00:00:00 2001 From: bibed234 Date: Sun, 15 Nov 2026 17:41:33 +0200 Subject: [PATCH 3/3] Fix missing io import and STORAGE_DIR in video_stickers.py --- video_stickers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/video_stickers.py b/video_stickers.py index 20baef4..092e232 100644 --- a/video_stickers.py +++ b/video_stickers.py @@ -1,8 +1,11 @@ +import io import os import re from video_text import TITLE_FONT_PATH, _with_shadow +STORAGE_DIR = "/tmp/automap_storage" + from sticker_themes import ( ALLOWED_STICKERS, TWEMOJI_CODEPOINTS,