"""Side-by-side preview of a codec_study clip: source | MD build / 32X 4 MB | 32X mapper. Writes a 2x2 frame sequence (640 x 480 with labels) and an animated WebP at 25 fps. py -3.12 study/preview.py --first 0 --count 375 --out out32/preview_0 """ from __future__ import annotations import argparse import sys from pathlib import Path import numpy as np from PIL import Image, ImageDraw sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from rrmd.encode import load_picture # noqa: E402 FIELDS = Path(r"D:\dev\rick-roll-md\fields") MD_EXPECTED = Path(r"D:\dev\rick-roll-md\out\full-lloyd\expected") LABEL_H = 16 def panel(img: np.ndarray, text: str) -> Image.Image: im = Image.new("RGB", (320, 224 + LABEL_H), (0, 0, 0)) im.paste(Image.fromarray(img), (0, LABEL_H)) ImageDraw.Draw(im).text((4, 2), text, fill=(255, 255, 255)) return im def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--first", type=int, required=True) ap.add_argument("--count", type=int, default=375) ap.add_argument("--low", type=Path, help="codec_study out dir for the 4 MB budget") ap.add_argument("--high", type=Path, help="codec_study out dir for the mapper budget") ap.add_argument("--out", type=Path, required=True) a = ap.parse_args() low = a.low or Path(f"out32/full_{a.first}_675") high = a.high or Path(f"out32/full_{a.first}_3000") a.out.mkdir(parents=True, exist_ok=True) paths = sorted(FIELDS.glob("f*.png")) frames = [] for i in range(a.first, a.first + a.count): src = load_picture(paths[i]) md = np.asarray(Image.open(MD_EXPECTED / f"e{i:05d}.png").convert("RGB")) lo = np.asarray(Image.open(low / f"p{i:05d}.png")) hi = np.asarray(Image.open(high / f"p{i:05d}.png")) sheet = Image.new("RGB", (640, 2 * (224 + LABEL_H))) sheet.paste(panel(src, f"source {i}"), (0, 0)) sheet.paste(panel(md, "MD build: 2,340 B/f, 16 MB"), (320, 0)) sheet.paste(panel(lo, "32X C1 8bpp: 675 B/f (4 MB)"), (0, 224 + LABEL_H)) sheet.paste(panel(hi, "32X C1 8bpp: 3,000 B/f (mapper)"), (320, 224 + LABEL_H)) sheet.save(a.out / f"s{i:05d}.png") frames.append(sheet) frames[0].save(a.out / "preview.webp", save_all=True, append_images=frames[1:], duration=40, loop=0, quality=90, method=4) print(a.out / "preview.webp") if __name__ == "__main__": main()