Skillfarm / skills · skillfarm.ai / converted / pdf / scripts / create_validation_image.py
| 1 | import json |
| 2 | import sys |
| 3 | |
| 4 | from PIL import Image, ImageDraw |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | def create_validation_image(page_number, fields_json_path, input_path, output_path): |
| 10 | with open(fields_json_path, 'r') as f: |
| 11 | data = json.load(f) |
| 12 | |
| 13 | img = Image.open(input_path) |
| 14 | draw = ImageDraw.Draw(img) |
| 15 | num_boxes = 0 |
| 16 | |
| 17 | for field in data["form_fields"]: |
| 18 | if field["page_number"] == page_number: |
| 19 | entry_box = field['entry_bounding_box'] |
| 20 | label_box = field['label_bounding_box'] |
| 21 | draw.rectangle(entry_box, outline='red', width=2) |
| 22 | draw.rectangle(label_box, outline='blue', width=2) |
| 23 | num_boxes += 2 |
| 24 | |
| 25 | img.save(output_path) |
| 26 | print(f"Created validation image at {output_path} with {num_boxes} bounding boxes") |
| 27 | |
| 28 | |
| 29 | if __name__ == "__main__": |
| 30 | if len(sys.argv) != 5: |
| 31 | print("Usage: create_validation_image.py [page number] [fields.json file] [input image path] [output image path]") |
| 32 | sys.exit(1) |
| 33 | page_number = int(sys.argv[1]) |
| 34 | fields_json_path = sys.argv[2] |
| 35 | input_image_path = sys.argv[3] |
| 36 | output_image_path = sys.argv[4] |
| 37 | create_validation_image(page_number, fields_json_path, input_image_path, output_image_path) |