"""
Crop a question row from a rendered PDF page PNG.

Usage:
  python crop_question_row.py <page_png> <x0> <y0> <x1> <y1> <out_png>

  Coordinates are NORMALIZED (0.0-1.0) relative to page dimensions.
  Add --padding 0.01 for extra border (default 0.005).

Example:
  python crop_question_row.py page_005.png 0.0 0.12 1.0 0.22 2019_v1_a7.png
"""
import sys
import argparse
from PIL import Image

def crop(page_png, x0, y0, x1, y1, out_png, padding=0.005):
    img = Image.open(page_png)
    w, h = img.size
    px0 = max(0, int((x0 - padding) * w))
    py0 = max(0, int((y0 - padding) * h))
    px1 = min(w, int((x1 + padding) * w))
    py1 = min(h, int((y1 + padding) * h))
    cropped = img.crop((px0, py0, px1, py1))
    cropped.save(out_png)
    print(f'Saved {out_png} ({cropped.width}x{cropped.height})')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('page_png')
    parser.add_argument('x0', type=float)
    parser.add_argument('y0', type=float)
    parser.add_argument('x1', type=float)
    parser.add_argument('y1', type=float)
    parser.add_argument('out_png')
    parser.add_argument('--padding', type=float, default=0.005)
    args = parser.parse_args()
    crop(args.page_png, args.x0, args.y0, args.x1, args.y1, args.out_png, args.padding)
