summaryrefslogtreecommitdiff
path: root/scripts/make-p45-char-crop.py
blob: 430bc72ea9bc781a9d42d0fb85cbd55d6a46f19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""P45 evidence: crop the avatar cell from the real LV_P45_SHOT frames and upscale 4x.

Reads the shot log for the character destination rect printed by the game
(`dest=x,y,w,h`), crops that region (plus a small margin) from the full 1280x720
screenshot and upscales exactly 4x with NEAREST. Pure function of real frames.

Usage:
    python3 scripts/make-p45-char-crop.py <record-dir> <shot-log> <out-dir>
"""

import re
import sys
from pathlib import Path

from PIL import Image

MARGIN = 8
SCALE = 4
DEST_RE = re.compile(r"p45-shot=(\S+) tick=(\d+) facing=(\S+) moving=(\w+) dest=(\d+),(\d+),(\d+),(\d+) frame=(.+)")


def main(argv):
    if len(argv) != 4:
        sys.stderr.write(__doc__)
        return 2
    record_dir = Path(argv[1])
    log_text = Path(argv[2]).read_text(encoding="utf-8")
    out_dir = Path(argv[3])
    out_dir.mkdir(parents=True, exist_ok=True)
    count = 0
    for line in log_text.splitlines():
        m = DEST_RE.search(line)
        if not m:
            continue
        name = m.group(1)
        x, y, w, h = (int(m.group(i)) for i in range(5, 9))
        box = (x - MARGIN, y - MARGIN, x + w + MARGIN, y + h + MARGIN)
        source = Image.open(record_dir / f"{name}.png").convert("RGBA")
        crop = source.crop(box)
        doubled = crop.resize((crop.width * SCALE, crop.height * SCALE), Image.NEAREST)
        doubled.save(out_dir / f"{name}.png")
        print(f"ok {name}.png {doubled.width}x{doubled.height} from {box} (facing={m.group(3)} moving={m.group(4)} frame={m.group(9)})")
        count += 1
    if count == 0:
        sys.stderr.write("no p45-shot dest lines found\n")
        return 1
    return 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv))