#!/usr/bin/env python3
"""Remove background from mascot.gif frame-by-frame using rembg."""

import sys
from pathlib import Path

try:
    from rembg import remove
    from PIL import Image
except ImportError:
    print("Missing dependencies. Install with:")
    print("  pip install rembg Pillow")
    sys.exit(1)

INPUT = Path(__file__).resolve().parent.parent / "assets" / "mascot.gif"
OUTPUT = Path(__file__).resolve().parent.parent / "assets" / "mascot_nobg.gif"


def main():
    if not INPUT.exists():
        print(f"Input not found: {INPUT}")
        sys.exit(1)

    print(f"Reading {INPUT}...")
    original = Image.open(INPUT)

    frames = []
    durations = []
    try:
        while True:
            durations.append(original.info.get("duration", 100))
            frame = original.convert("RGBA")
            print(f"  Processing frame {len(frames) + 1}...")
            frames.append(remove(frame))
            original.seek(original.tell() + 1)
    except EOFError:
        pass

    print(f"Saving {len(frames)} frames to {OUTPUT}...")
    frames[0].save(
        OUTPUT,
        save_all=True,
        append_images=frames[1:],
        duration=durations,
        loop=0,
        disposal=2,
    )
    print(f"Done → {OUTPUT}")


if __name__ == "__main__":
    main()
