import cv2, json

for pnum in (1, 2, 3):
    img = cv2.imread(f"pdf/page-{pnum}.png")
    rows = json.load(open(f"boxes_page{pnum}.json"))
    DPI = 150
    PX_PER_PT = DPI / 72.0
    for ri, row in enumerate(rows):
        c0 = row["cells"][0]
        x_px = int(c0["x"] * PX_PER_PT)
        y_px = int(c0["y"] * PX_PER_PT)
        cv2.putText(img, f"R{ri}", (max(0, x_px - 45), y_px + 15),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        cv2.rectangle(img, (x_px, y_px), (x_px + int(c0["w"]*PX_PER_PT)*row["cell_count"], y_px + int(c0["h"]*PX_PER_PT)), (255,0,0), 1)
    cv2.imwrite(f"pdf/labeled-{pnum}.png", img)
print("done")
