PulseCanvas / app · pulsecanvas.ai / scripts / native-contact-sheet.py
| 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 | """ |
| 7 | import argparse |
| 8 | import json |
| 9 | import subprocess |
| 10 | import sys |
| 11 | from pathlib import Path |
| 12 | |
| 13 | from PIL import Image, ImageDraw, ImageFont |
| 14 | |
| 15 | root = Path(__file__).resolve().parents[1] |
| 16 | |
| 17 | |
| 18 | def 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 | |
| 23 | def 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 | |
| 32 | def 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 | |
| 40 | def 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 | |
| 55 | parser = argparse.ArgumentParser() |
| 56 | parser.add_argument('--input', type=Path) |
| 57 | parser.add_argument('--strip', type=Path) |
| 58 | parser.add_argument('--frames', type=int, default=6) |
| 59 | parser.add_argument('--columns', type=int, default=6) |
| 60 | parser.add_argument('--scale', type=float, default=1.0) |
| 61 | parser.add_argument('--fps', type=float, default=30) |
| 62 | parser.add_argument('--output', type=Path, required=True) |
| 63 | args = parser.parse_args() |
| 64 | names = effect_names() |
| 65 | if 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) |
| 69 | elif 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) |