Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / native-contact-sheet.py

Raw 3.2 KB on 15681b389943bdb82ce0ee35ea3df6538f046361
1#!/usr/bin/env python3
2"""Compose labeled contact sheets from gallery stills (NNN.png) or motion strips.
3
4 scripts/native-contact-sheet.py --input .build/gallery/wide --output sheet.png
5 scripts/native-contact-sheet.py --strip .build/gallery/frames/040 --frames 6 --output strip.png
6"""
7import argparse
8import json
9import subprocess
10import sys
11from pathlib import Path
12
13from PIL import Image, ImageDraw, ImageFont
14
15root = Path(__file__).resolve().parents[1]
16
17
18def effect_names():
19 catalog = subprocess.check_output([sys.executable, str(root / 'scripts/native-visualizer-catalog.py')], text=True)
20 return {effect['scene']: effect['name'] for effect in json.loads(catalog)['effects'] if not effect.get('retired')}
21
22
23def font(size):
24 for candidate in ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/Helvetica.ttc']:
25 try:
26 return ImageFont.truetype(candidate, size)
27 except OSError:
28 continue
29 return ImageFont.load_default()
30
31
32def label(image, text, size):
33 draw = ImageDraw.Draw(image, 'RGBA')
34 face = font(size)
35 box = draw.textbbox((0, 0), text, font=face)
36 draw.rectangle((0, image.height - (box[3] - box[1]) - size, box[2] - box[0] + size, image.height), fill=(0, 0, 0, 170))
37 draw.text((size // 2, image.height - (box[3] - box[1]) - size // 2 - box[1]), text, font=face, fill=(255, 255, 255, 255))
38
39
40def sheet(paths, captions, columns, output, scale):
41 tiles = [Image.open(p).convert('RGB') for p in paths]
42 width, height = int(tiles[0].width * scale), int(tiles[0].height * scale)
43 rows = (len(tiles) + columns - 1) // columns
44 gap = 6
45 canvas = Image.new('RGB', (columns * width + (columns + 1) * gap, rows * height + (rows + 1) * gap), (12, 12, 16))
46 for index, (tile, caption) in enumerate(zip(tiles, captions)):
47 tile = tile.resize((width, height), Image.LANCZOS)
48 label(tile, caption, max(11, height // 14))
49 x, y = index % columns, index // columns
50 canvas.paste(tile, (gap + x * (width + gap), gap + y * (height + gap)))
51 canvas.save(output)
52 print(f'Wrote {output} ({len(tiles)} tiles)')
53
54
55parser = argparse.ArgumentParser()
56parser.add_argument('--input', type=Path)
57parser.add_argument('--strip', type=Path)
58parser.add_argument('--frames', type=int, default=6)
59parser.add_argument('--columns', type=int, default=6)
60parser.add_argument('--scale', type=float, default=1.0)
61parser.add_argument('--fps', type=float, default=30)
62parser.add_argument('--output', type=Path, required=True)
63args = parser.parse_args()
64names = effect_names()
65if args.input:
66 paths = sorted(p for p in args.input.glob('[0-9][0-9][0-9].png') if int(p.stem) in names)
67 captions = [f'{int(p.stem)} {names.get(int(p.stem), "?")}' for p in paths]
68 sheet(paths, captions, args.columns, args.output, args.scale)
69elif args.strip:
70 frames = sorted(args.strip.glob('*.png'))
71 picks = [frames[round(i * (len(frames) - 1) / max(1, args.frames - 1))] for i in range(args.frames)]
72 scene = int(args.strip.name)
73 captions = [f'{names.get(scene, "?")} t+{int(p.stem) / args.fps:.2f}s' for p in picks]
74 sheet(picks, captions, args.frames if args.columns >= args.frames else args.columns, args.output, args.scale)