main
 1import json
 2import sys
 3
 4from PIL import Image, ImageDraw
 5
 6
 7# Creates "validation" images with rectangles for the bounding box information that
 8# Claude creates when determining where to add text annotations in PDFs. See forms.md.
 9
10
11def create_validation_image(page_number, fields_json_path, input_path, output_path):
12    # Input file should be in the `fields.json` format described in forms.md.
13    with open(fields_json_path, 'r') as f:
14        data = json.load(f)
15
16        img = Image.open(input_path)
17        draw = ImageDraw.Draw(img)
18        num_boxes = 0
19        
20        for field in data["form_fields"]:
21            if field["page_number"] == page_number:
22                entry_box = field['entry_bounding_box']
23                label_box = field['label_bounding_box']
24                # Draw red rectangle over entry bounding box and blue rectangle over the label.
25                draw.rectangle(entry_box, outline='red', width=2)
26                draw.rectangle(label_box, outline='blue', width=2)
27                num_boxes += 2
28        
29        img.save(output_path)
30        print(f"Created validation image at {output_path} with {num_boxes} bounding boxes")
31
32
33if __name__ == "__main__":
34    if len(sys.argv) != 5:
35        print("Usage: create_validation_image.py [page number] [fields.json file] [input image path] [output image path]")
36        sys.exit(1)
37    page_number = int(sys.argv[1])
38    fields_json_path = sys.argv[2]
39    input_image_path = sys.argv[3]
40    output_image_path = sys.argv[4]
41    create_validation_image(page_number, fields_json_path, input_image_path, output_image_path)