import fitz

BASE = r'C:\allgifted\mathapi11v2\docs\past year questions\P1\_ingest\henry-park-2024-quiz1'
PDF = BASE + r'\paper.pdf'
doc = fitz.open(PDF)

ZOOM = 3
mat = fitz.Matrix(ZOOM, ZOOM)

# Each entry: output filename -> (page_index, fitz.Rect(x0,y0,x1,y1))
# Page is A4 (~595 x 842 pt). Rects estimated from layout, exclude question
# text and the www.sgexam.com watermark; verify by viewing and adjust.
crops = {
    # Page 0 (idx 0): Q1 ducks box, roughly mid-page
    'q1.png':  (0, fitz.Rect(120, 520, 530, 670)),
    # Page 1 (idx 1): Q3 Set A / Set B table (two rows), Q4 burgers+drinks
    'q3_opt0.png': (1, fitz.Rect(120, 255, 470, 320)),   # Set A row (stars)
    'q3_opt1.png': (1, fitz.Rect(120, 320, 470, 390)),   # Set B row (moons)
    'q4.png':  (1, fitz.Rect(120, 445, 540, 670)),
    # Page 2 (idx 2): Q5 lollipops/sweets box, Q6 three pots
    'q5.png':  (2, fitz.Rect(120, 150, 510, 280)),
    'q6.png':  (2, fitz.Rect(150, 430, 460, 520)),
    # Page 3 (idx 3): Q9 four crossed dinosaurs
    'q9.png':  (3, fitz.Rect(110, 360, 470, 450)),
    # Page 4 (idx 4): Q10 watermelons, Q11 three sheep
    'q10.png': (4, fitz.Rect(120, 170, 530, 240)),
    'q11.png': (4, fitz.Rect(120, 430, 530, 540)),
    # Page 5 (idx 5): Q12 cars+bicycles, Q13 t-shirt pattern
    'q12.png': (5, fitz.Rect(120, 210, 470, 380)),
    'q13.png': (5, fitz.Rect(90, 560, 560, 640)),
    # Page 6 (idx 6): Q14 shells
    'q14.png': (6, fitz.Rect(140, 280, 540, 490)),
    # Page 7 (idx 7): Q15 cupcakes on plate
    'q15.png': (7, fitz.Rect(120, 200, 520, 420)),
    # Page 8 (idx 8): Q16 man carrying boxes
    'q16.png': (8, fitz.Rect(120, 230, 530, 470)),
}

for fn, (pi, rect) in crops.items():
    page = doc[pi]
    pix = page.get_pixmap(matrix=mat, clip=rect)
    out = BASE + '\\' + fn
    pix.save(out)
    print('wrote', fn, pix.width, 'x', pix.height)

print('done')
