microduck-electric-slide-motion / scripts /make_simple_contact_sheet.py
Histochemichael's picture
Add simple-v3 16-count Microduck Electric Slide dataset
05f9189 verified
Raw History Blame Contribute Delete
3.36 kB
"""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()