# -*- coding: utf-8 -*-
"""测试ocr的位置，与预期是否一致"""
from PIL import Image, ImageDraw
import json
from pathlib import Path


def draw_rectangles_on_image(image_path, rectangles, output_path):
   
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)

   
    for rectangle in rectangles:
        top_left = (rectangle['left'], rectangle['top'])
        bottom_right = (rectangle['left'] + rectangle['width'], rectangle['top'] + rectangle['height'])
        draw.rectangle([top_left, bottom_right], outline='red', width=2)

   
    image.save(output_path)


rectangles = [
   
   
   
]

with open("log.txt", "r", encoding="utf-8") as f:
    try:
        ocr_data = json.loads(f.read())
    except json.decoder.JSONDecodeError:
        print("json格式化错误")

for i in ocr_data['words_result']:
    for char_loca in i['chars']:
        rectangles.append(char_loca['location'])

script_path = Path(__file__).resolve()
script_directory = script_path.parent
transformed_image_path = str(Path(script_directory, r"transformed_image.jpg"))
draw_rectangles_on_image(transformed_image_path, rectangles, 'output_with_rectangles.jpg')