Skip to content

艺术动画skill:35种艺术风格、9种解说语法,用代码让画动起来。

3.1 KB
Raw Blame
#!/usr/bin/env python3
"""讲解员帧:绿幕 sprite 表 → 抠图切帧 → 量脚底中心与「帽顶到脚底」身高 → spacex_lin/assets/p_<姿势>_<i>.png + spacex_lin/presenter_meta.js
用法:python3 prep_presenter.py   (扫 assets/src/sheet_*.png,外加混合版 s11 的指向表与已切好的惊讶帧)"""
from pathlib import Path
import json
import numpy as np
from PIL import Image

ROOT = Path(__file__).resolve().parents[1]; SRC = ROOT / 'assets' / 'src'; OUT = ROOT / 'assets'
MIX = ROOT.parent / 'spacex' / 'assets'
SHEETS = {p.stem[6:]: p for p in sorted(SRC.glob('sheet_*.png'))}
SHEETS.setdefault('point', MIX / 's11_sheet_point.png')
PRECUT = {'wow': [MIX / f's11_wow_{i}.png' for i in range(3)]}   # 混合版已切好(三帧手臂相连,按列切不开)
EXPECT = {'walk': 6, 'bust': 4}


def key(im):
    a = np.asarray(im.convert('RGB')).astype(np.float32); r, g, b = a[..., 0], a[..., 1], a[..., 2]
    spill = g - np.maximum(r, b); al = 1 - np.clip((spill - 30) / 70, 0, 1)
    g2 = np.where(spill > 0, np.minimum(g, np.maximum(r, b) + 0.25 * np.clip(spill, 0, 30)), g)
    return np.dstack([r, g2, b, al * 255]).clip(0, 255).astype(np.uint8)


def runs(mask):
    cols = mask.sum(0) > 2; out = []; x = 0; W = len(cols)
    while x < W:
        if cols[x]:
            x0 = x
            while x < W and cols[x:x + 14].any(): x += 1
            if x - x0 > 60: out.append((x0, x))
        x += 1
    if out:
        mw = max(b - a for a, b in out); out = [r for r in out if r[1] - r[0] >= 0.45 * mw]   # 汗珠、强调线这类碎块不算一帧
    return out


def crop(fr):
    m = fr[..., 3] > 127; ys = np.where(m.any(1))[0]; xs = np.where(m.any(0))[0]
    return fr[ys.min():ys.max() + 1, xs.min():xs.max() + 1]


def measure(fr):
    m = fr[..., 3] > 127; h, w = m.shape
    bot = m[int(h * 0.94):]; xs = np.where(bot)[1]; ax = float(xs.mean()) if len(xs) else w / 2   # 脚底中心
    white = (fr[..., :3].astype(int).min(2) > 228) & m; cnt = white.sum(1)                          # 帽顶:第一行近白像素 ≥ 40
    top = int(np.argmax(cnt >= 40)) if (cnt >= 40).any() else 0
    return w, h, round(ax, 1), top


meta = {}
jobs = [(p, None, s) for p, s in SHEETS.items()] + [(p, l, None) for p, l in PRECUT.items()]
for pose, pre, sheet in jobs:
    if pre: frames = [crop(np.asarray(Image.open(q).convert('RGBA'))) for q in pre]
    else:
        rgba = key(Image.open(sheet)); mask = rgba[..., 3] > 127
        frames = [crop(rgba[:, x0:x1]) for x0, x1 in runs(mask)]
    meta[pose] = []
    for k, fr in enumerate(frames):
        w, h, ax, top = measure(fr); name = f'p_{pose}_{k}.png'
        Image.fromarray(fr, 'RGBA').save(OUT / name)
        meta[pose].append({'src': f'spacex_lin/assets/{name}', 'w': w, 'h': h, 'ax': ax, 'ay': h, 'top': top, 'body': h - top})
    print(pose, len(frames), [(f['w'], f['h'], f['body']) for f in meta[pose]], '' if len(frames) == EXPECT.get(pose, 3) else '⚠️ 帧数不对')
(ROOT / 'presenter_meta.js').write_text('// 由 tools/prep_presenter.py 生成,勿手改\nwindow.LIN_META = ' + json.dumps(meta, ensure_ascii=False) + ';\n')