"""Strip the (N positions) counts from the legend in exp3_leftright_distribution.png.

The existing PNG (448×448) has a legend block in the top ~95 px reading:
    Left half (j=0-7)
    ● Train (128 positions)
    ● Test (20 positions)
    All 256 grid positions

The image background underneath the legend is the same LIBERO wood-table render
that fills the rest of the frame. We extract that background from a clean band
on the lower-left of the image, paint over the legend area with it, then re-draw
a compact "Train / Test" legend (no counts).

Output is written in place — the file used by build_fig4_ood.py is also updated.
"""
import argparse
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont


def find_font(size: int) -> ImageFont.FreeTypeFont:
    candidates = [
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
        "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
    ]
    for p in candidates:
        if Path(p).exists():
            return ImageFont.truetype(p, size)
    return ImageFont.load_default()


def main():
    ap = argparse.ArgumentParser()
    # The meeting-notes copy is treated as the known-good ORIGINAL (with the
    # "(128 positions)" / "(20 positions)" legend). Read from there so this
    # script is idempotent — re-running won't compound the legend rewrite.
    ap.add_argument("--input", default="/data/cameron/para/.agents/reports/meeting_notes/2026-04-09-para-status/media/exp3_leftright_distribution.png")
    ap.add_argument("--outputs", nargs="+",
                    default=[
                        "/data/cameron/para/.agents/reports/project_site/media/exp3_leftright_distribution.png",
                        "/data/cameron/para/.agents/reports/backbones/media/exp3_leftright_distribution.png",
                    ])
    args = ap.parse_args()

    src = Image.open(args.input).convert("RGBA")
    W, H = src.size

    # The original PNG has a baked-in legend ("Left half (j=0-7)" + Train/Test
    # counts) in the top ~95 px and we don't have the script that produced it.
    # Per Cameron: drop the legend block entirely and keep only the scene-+-dots
    # portion that lived below it. Crop the top off; the SVG embed slot will
    # slice the result to fit.
    legend_h = 96
    out = src.crop((0, legend_h, W, H))

    for o in args.outputs:
        Path(o).parent.mkdir(parents=True, exist_ok=True)
        out.convert("RGB").save(o, "PNG")
        print(f"wrote {o}  ({out.size[0]}×{out.size[1]})")


if __name__ == "__main__":
    main()
