#!/usr/bin/env python3
"""Check existing cutouts quality - are they actually background-removed or just copies?"""
import os, sys, random
from pathlib import Path
from PIL import Image

cutout_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("D:/sneakerpicks-cutouts")

files = list(cutout_dir.glob("*.png"))
print(f"Total cutout files: {len(files)}")

# Sample random 50
sample = random.sample(files, min(50, len(files)))

good = 0
bad = 0
for f in sample:
    try:
        img = Image.open(f)
        if img.mode != "RGBA":
            print(f"  {f.stem}: NO ALPHA (mode={img.mode}) — NOT processed")
            bad += 1
            continue
        alpha = img.split()[3]
        data = list(alpha.getdata())
        transparent = sum(1 for p in data if p < 10)
        pct = transparent / len(data) * 100
        if pct < 5:
            print(f"  {f.stem}: {pct:.1f}% transparent — likely NOT bg-removed")
            bad += 1
        else:
            print(f"  {f.stem}: {pct:.1f}% transparent — OK")
            good += 1
    except Exception as e:
        print(f"  {f.stem}: ERROR {e}")
        bad += 1

print(f"\nSample results: {good} good, {bad} bad out of {len(sample)}")
