File size: 3,360 Bytes
05f9189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Create labeled v3 example images from the final split-camera render."""

from __future__ import annotations

import argparse
from pathlib import Path
import sys

import imageio.v2 as imageio
from PIL import Image, ImageDraw, ImageFont, ImageOps

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from choreography import BEAT_SECONDS
from simple_choreography import SIMPLE_COUNTS


def font(size: int) -> ImageFont.ImageFont:
    for candidate in ("C:/Windows/Fonts/segoeuib.ttf", "C:/Windows/Fonts/arialbd.ttf"):
        try:
            return ImageFont.truetype(candidate, size=size)
        except OSError:
            pass
    return ImageFont.load_default()


def labeled(image: Image.Image, label: str, banner_height: int) -> Image.Image:
    output = Image.new("RGB", (image.width, image.height + banner_height), "#f7f9fc")
    output.paste(image, (0, 0))
    draw = ImageDraw.Draw(output)
    draw.rectangle((0, image.height, image.width, output.height), fill="#172033")
    font_size = max(16, banner_height // 2)
    label_font = font(font_size)
    while draw.textbbox((0, 0), label, font=label_font)[2] > image.width - 28 and font_size > 11:
        font_size -= 1
        label_font = font(font_size)
    draw.text((14, image.height + banner_height // 2), label, fill="white", font=label_font, anchor="lm")
    return output


def frame_at(reader: imageio.Reader, fps: float, time_seconds: float) -> Image.Image:
    return Image.fromarray(reader.get_data(int(round(time_seconds * fps)))).convert("RGB")


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("video", type=Path)
    parser.add_argument("output_dir", type=Path)
    args = parser.parse_args()
    output_dir = args.output_dir.resolve()
    output_dir.mkdir(parents=True, exist_ok=True)

    reader = imageio.get_reader(str(args.video.resolve()))
    try:
        fps = float(reader.get_meta_data()["fps"])
        cells: list[Image.Image] = []
        for count, spec in enumerate(SIMPLE_COUNTS):
            frame = frame_at(reader, fps, (count + 0.5) * BEAT_SECONDS)
            close = ImageOps.fit(frame.crop((0, 0, frame.width // 2, frame.height)), (400, 225), Image.Resampling.LANCZOS)
            cells.append(labeled(close, f"{count + 1}. {spec.label}", 48))

        sheet = Image.new("RGB", (1600, 4 * cells[0].height), "white")
        for index, item in enumerate(cells):
            sheet.paste(item, ((index % 4) * 400, (index // 4) * item.height))
        sheet.save(output_dir / "simple_16_count_contact_sheet.jpg", quality=92)

        examples = {
            "example_right_tap_head_bob": 4,
            "example_left_tap_head_bob": 8,
            "example_three_steps_back": 10,
            "example_rock_forward": 13,
            "example_rock_back": 14,
            "example_quarter_turn": 16,
        }
        for filename, one_based_count in examples.items():
            spec = SIMPLE_COUNTS[one_based_count - 1]
            frame = frame_at(reader, fps, (one_based_count - 0.5) * BEAT_SECONDS)
            frame.thumbnail((1400, 630), Image.Resampling.LANCZOS)
            labeled(frame, f"Count {one_based_count}: {spec.label}", 64).save(
                output_dir / f"{filename}.jpg", quality=92
            )
    finally:
        reader.close()


if __name__ == "__main__":
    main()