diff --git a/reading_annotations.py b/reading_annotations.py index c11b29b..fe9acde 100644 --- a/reading_annotations.py +++ b/reading_annotations.py @@ -255,7 +255,7 @@ def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_laye (base_img, _, _) = annotating.make_base_image(pdf_path) # Compose uses the result object we modified in step 1 - final_img, _ = annotating.compose_label_image( + final_img, new_header_h = annotating.compose_label_image( base_img, label, content['result'], content['coordinates'][0], with_error=False ) @@ -264,7 +264,18 @@ def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_laye # Overlay manual notes if has_notes: - final_img.paste(sub_note, (0, 0), mask=sub_note) + old_header_h = int(img_info.get("header_height", 0)) + w, h = sub_note.size + + # 1. Paste header ink at the top + if old_header_h > 0: + header_crop = sub_note.crop((0, 0, w, min(h, old_header_h))) + final_img.paste(header_crop, (0, 0), mask=header_crop) + + # 2. Paste student-content ink at the new header height + if h > old_header_h: + body_crop = sub_note.crop((0, old_header_h, w, h)) + final_img.paste(body_crop, (0, new_header_h), mask=body_crop) # C. Save individual file if Modified (Dirty logic or visual notes) if (label in dirty_labels) or has_notes: diff --git a/reading_grouped_annotations.py b/reading_grouped_annotations.py index af4001f..b74260c 100644 --- a/reading_grouped_annotations.py +++ b/reading_grouped_annotations.py @@ -81,7 +81,7 @@ def apply_actions_and_regenerate_grouped(root_dir, data, student_id, actions, la (base_img, _, _) = annotating.make_base_image(pdf_path) # Compose uses the result object we modified in step 1 - final_img, _ = annotating.compose_label_image( + final_img, new_header_h = annotating.compose_label_image( base_img, label, content['result'], content['coordinates'][0], with_error=False ) @@ -91,10 +91,23 @@ def apply_actions_and_regenerate_grouped(root_dir, data, student_id, actions, la # Overlay manual notes specific to this label has_notes = False if label in label_notes: - sub_note = label_notes[label] + note_info = label_notes[label] + sub_note = note_info['img'] + old_header_h = int(note_info['old_header_h']) + if has_significant_notes(sub_note): has_notes = True - final_img.paste(sub_note, (0, 0), mask=sub_note) + w, h = sub_note.size + + # 1. Paste header ink at the top + if old_header_h > 0: + header_crop = sub_note.crop((0, 0, w, min(h, old_header_h))) + final_img.paste(header_crop, (0, 0), mask=header_crop) + + # 2. Paste student-content ink at the new header height + if h > old_header_h: + body_crop = sub_note.crop((0, old_header_h, w, h)) + final_img.paste(body_crop, (0, new_header_h), mask=body_crop) # Save individual file if Modified (Dirty logic or visual notes) if (label in dirty_labels) or has_notes: @@ -205,7 +218,10 @@ if __name__ == "__main__": crop = notes_img.crop((0, hmin, notes_img.width, hmax)) # Store it if there are pen marks on it if has_significant_notes(crop): - notes_by_student[sid][lbl] = crop + notes_by_student[sid][lbl] = { + 'img': crop, + 'old_header_h': img_info.get("header_height", 0) + } # --- 2. Dispatch data back to students and regenerate --- # affected_students = set(actions_by_student.keys()).union(set(notes_by_student.keys()))