#!/usr/bin/env python3
"""Composite an illustration onto the right side of a slide image."""
import sys
from PIL import Image

def composite(slide_path, illustration_path, output_path, right_margin=40, bottom_margin=20):
    slide = Image.open(slide_path).convert("RGBA")
    illust = Image.open(illustration_path).convert("RGBA")
    
    sw, sh = slide.size
    iw, ih = illust.size
    
    # Scale illustration to fit right half of slide, max 55% width, max 85% height
    max_w = int(sw * 0.55)
    max_h = int(sh * 0.85)
    
    scale = min(max_w / iw, max_h / ih)
    new_w = int(iw * scale)
    new_h = int(ih * scale)
    
    illust_resized = illust.resize((new_w, new_h), Image.LANCZOS)
    
    # Position on right side, vertically centered (slightly lower)
    x = sw - new_w - right_margin
    y = (sh - new_h) // 2 + int(sh * 0.05)
    
    # Composite
    result = slide.copy()
    result.paste(illust_resized, (x, y), illust_resized)
    
    result.convert("RGB").save(output_path, quality=95)
    print(f"Saved: {output_path}")

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