#!/usr/bin/env python3
"""Composite a portrait illustration as a right-side panel on a slide."""
import sys
from PIL import Image

def composite_panel(slide_path, illustration_path, output_path):
    slide = Image.open(slide_path).convert("RGBA")
    illust = Image.open(illustration_path).convert("RGBA")
    
    sw, sh = slide.size
    iw, ih = illust.size
    
    # Panel takes up right ~47% of slide, full height
    panel_w = int(sw * 0.47)
    panel_h = sh
    
    # Scale illustration to fill the panel
    scale = max(panel_w / iw, panel_h / ih)
    new_w = int(iw * scale)
    new_h = int(ih * scale)
    
    illust_resized = illust.resize((new_w, new_h), Image.LANCZOS)
    
    # Center-crop to panel size
    left = (new_w - panel_w) // 2
    top = (new_h - panel_h) // 2
    illust_cropped = illust_resized.crop((left, top, left + panel_w, top + panel_h))
    
    # Place on right side of slide
    result = slide.copy()
    x = sw - panel_w
    result.paste(illust_cropped, (x, 0))
    
    result.convert("RGB").save(output_path, quality=95)
    print(f"Saved: {output_path} ({result.size[0]}x{result.size[1]})")

if __name__ == "__main__":
    composite_panel(sys.argv[1], sys.argv[2], sys.argv[3])
