Working state, mostly…

This commit is contained in:
2026-02-09 22:45:04 +01:00
parent c94c773c10
commit 7656ca652f
3 changed files with 132 additions and 116 deletions
+8 -21
View File
@@ -15,7 +15,7 @@ from annotating import MARGIN_LEFT, ANNOT_WIDTH
# Global lock for Matplotlib/Latex rendering to prevent race conditions
LATEX_LOCK = threading.Lock()
DPI = 100
BOX_SIZE = 30
SCORE_BOX_SIZE = 30
SCORES = [x * 0.5 for x in range(9)] # 0.0 to 4.0
@@ -40,21 +40,6 @@ def safe_render_latex(text, **kwargs):
with LATEX_LOCK:
return annotating.render_latex_text(text, **kwargs)
def create_base_image(pdf_path):
"""Converts PDF pages to a single vertically stacked image."""
try:
pages = annotating.convert_from_path(pdf_path)
total_h = sum(page.height for page in pages)
max_w = max(page.width for page in pages)
base_img = Image.new("RGBA", (max_w, total_h), "white")
cy = 0
for page in pages:
base_img.paste(page.convert("RGBA"), (0, cy))
cy += page.height
return base_img
except Exception:
return None
def render_header(label, score, feedbacks, base_width):
"""Generates the score line and global feedback elements."""
elements = []
@@ -101,7 +86,7 @@ def process_label(root_dir, student_id, label, content):
if not os.path.exists(pdf_path):
return None, []
base_img = create_base_image(pdf_path)
base_img, total_h, max_w = annotating.make_base_image(pdf_path)
if not base_img:
return None, []
@@ -156,11 +141,12 @@ def process_label(root_dir, student_id, label, content):
rect_cb_box = draw_checkbox(draw, target_xmax - BOX_SIZE, target_ymin, BOX_SIZE)
checkbox_map.append({
"type": "del_local", "label": label, "index": i,
"type": "del_local_rect", "label": label, "index": i,
"text_preview": fb['text'][:20], "final_box": rect_cb_box
})
txt_img_raw = safe_render_latex(fb['text'], width_px=ANNOT_WIDTH, bg_color=(255, 200, 200, 180), max_lines=3)
txt_img_raw = safe_render_latex(fb['text'], width_px=ANNOT_WIDTH,
bg_color=(255, 200, 200, 180), max_lines=3)
container_h = max(txt_img_raw.height, BOX_SIZE)
txt_img = Image.new("RGBA", (ANNOT_WIDTH, container_h), (255, 255, 255, 0))
txt_img.paste(txt_img_raw, (0, 0))
@@ -170,7 +156,8 @@ def process_label(root_dir, student_id, label, content):
center_y = (target_ymin + target_ymax) / 2
paste_y = max(center_y - (txt_img.height / 2), image_offset_y)
if paste_y < last_text_bottom: paste_y = last_text_bottom + 5
if paste_y < last_text_bottom:
paste_y = last_text_bottom + 5
req_h = int(paste_y + txt_img.height + 20)
if req_h > final_img.height:
@@ -238,7 +225,7 @@ def process_student(args):
json.dump(final_json_map, f, indent=2)
concat_img.save(os.path.join(output_dir, "Reference.png"))
concat_img.save(os.path.join(output_dir, "Concat.pdf"), "PDF", resolution=100.0)
concat_img.save(os.path.join(output_dir, "Concat.pdf"), "PDF", resolution=DPI)
if __name__ == "__main__":