Spaces:
Running
Running
File size: 869 Bytes
4596576 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from pathlib import Path
import csv, json, cv2
ROOT = Path(__file__).resolve().parents[1]
img = cv2.imread(str(ROOT / "template.jpg"))
config = json.loads((ROOT / "template.json").read_text(encoding="utf-8"))
bw, bh = config.get("bubbleDimensions", [60, 30])
answers = {}
import re
def start(block):
return int(re.search(r"q(\d+)", block["fieldLabels"][0]).group(1))
for block in sorted(config["fieldBlocks"].values(), key=start):
ox, oy = block["origin"]
for row in range(5):
q = start(block)+row
choice = (q-1)%4
answers[q] = "ABCD"[choice]
x = int(ox + choice*block["bubblesGap"]); y=int(oy+row*block["labelsGap"])
cv2.rectangle(img,(x+11,y+6),(x+bw-11,y+bh-6),(15,15,15),-1)
out = ROOT / "samples" / "sample_filled_clean_regenerated.jpg"
cv2.imwrite(str(out), img, [int(cv2.IMWRITE_JPEG_QUALITY),95])
print(out)
|