#!/usr/bin/env python3
"""Crop the catch image to GitHub header format."""

from PIL import Image
import os

img = Image.open("/root/.openclaw/workspace/output/bug-hunt-catch-v3-1.png")
w, h = img.size
print(f"Original: {w}x{h}")

# Target: 1280x400 (3.2:1 ratio)
target_w, target_h = 1280, 400
target_ratio = target_w / target_h

# Crop from center-ish vertically, keeping the puppet action
# The puppets are roughly in the middle-to-lower portion
# Crop height from 768 down to match 3.2:1 ratio at full width
crop_h = int(w / target_ratio)  # 1408 / 3.2 = 440px
print(f"Crop height: {crop_h}")

# Center the crop slightly below middle (puppets are mid-frame)
center_y = int(h * 0.50)  # slightly below center
top = center_y - crop_h // 2
bottom = top + crop_h

# Clamp
if top < 0:
    top = 0
    bottom = crop_h
if bottom > h:
    bottom = h
    top = h - crop_h

print(f"Crop box: (0, {top}, {w}, {bottom})")
cropped = img.crop((0, top, w, bottom))

# Resize to exact 1280x400
final = cropped.resize((target_w, target_h), Image.LANCZOS)
print(f"Final: {final.size[0]}x{final.size[1]}")

out = "/root/.openclaw/workspace/output/bug-hunt-github-header.png"
final.save(out, "PNG", optimize=True)
print(f"Saved: {out} ({os.path.getsize(out)} bytes)")

# Also save a slightly taller version (1280x480, ~2.67:1) in case they want more room
crop_h2 = int(w / (1280/480))
center_y2 = int(h * 0.50)
top2 = center_y2 - crop_h2 // 2
bottom2 = top2 + crop_h2
if top2 < 0: top2 = 0; bottom2 = crop_h2
if bottom2 > h: bottom2 = h; top2 = h - crop_h2
cropped2 = img.crop((0, top2, w, bottom2))
final2 = cropped2.resize((1280, 480), Image.LANCZOS)
out2 = "/root/.openclaw/workspace/output/bug-hunt-github-header-tall.png"
final2.save(out2, "PNG", optimize=True)
print(f"Saved: {out2} ({os.path.getsize(out2)} bytes)")
