#!/usr/bin/env python3
"""
list_image_sizes.py
-------------------
Scans the current directory for .jpg/.jpeg/.png files and writes a CSV:

Image Filename, Image Width, Image Height

Usage:
  python3 list_image_sizes.py
  # or choose a custom output file:
  python3 list_image_sizes.py --output my_images.csv
"""

import argparse
import csv
from pathlib import Path

def main():
    ap = argparse.ArgumentParser(description="List JPG/PNG image sizes in the current directory.")
    ap.add_argument("--output", default="image_sizes.csv", help="Output CSV filename (default: image_sizes.csv)")
    args = ap.parse_args()

    try:
        from PIL import Image  # requires Pillow
    except ImportError:
        raise SystemExit(
            "Pillow is required. Install with:\n  pip install --upgrade pillow"
        )

    here = Path(".").resolve()
    exts = {".jpg", ".jpeg", ".png"}
    imgs = sorted([p for p in here.iterdir() if p.is_file() and p.suffix.lower() in exts])

    out_path = here / args.output
    with out_path.open("w", encoding="utf-8", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Image Filename", "Image Width", "Image Height"])

        for img_path in imgs:
            try:
                with Image.open(img_path) as im:
                    w, h = im.size
                writer.writerow([img_path.name, w, h])
            except Exception as e:
                # If an image can't be opened, still record the filename with empty dims
                writer.writerow([img_path.name, "", ""])

    print(f"[ok] wrote {out_path} ({len(imgs)} images)")

if __name__ == "__main__":
    main()
