diff --git a/annotating_with_checks.py b/annotating_with_checks.py index 78ec7d4..b2593c4 100644 --- a/annotating_with_checks.py +++ b/annotating_with_checks.py @@ -4,6 +4,7 @@ import json import shutil import concurrent.futures import threading +import img2pdf # Fix for Matplotlib in threads: Set backend to non-interactive 'Agg' import matplotlib @@ -224,8 +225,17 @@ def process_student(args): with open(os.path.join(output_dir, "checkboxes.json"), "w") as f: 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=DPI) + + # Pour éviter du drift + temp_img_path = os.path.join(output_dir, "Reference.jpg") + concat_img.save(temp_img_path, quality=90) # Save as standard image first + with open(os.path.join(output_dir, "Concat.pdf"), "wb") as f: + f.write(img2pdf.convert(temp_img_path)) + + # Ancien code, avec du drift + # concat_img.save(os.path.join(output_dir, "Concat.pdf"), "PDF", resolution=DPI) + # concat_img.save(os.path.join(output_dir, "Reference.png")) + if __name__ == "__main__": diff --git a/reading_annotations.py b/reading_annotations.py index 1fd8225..3e4315b 100644 --- a/reading_annotations.py +++ b/reading_annotations.py @@ -17,7 +17,8 @@ def detect_checks_and_notes(output_dir): notes_img: RGBA image of manual notes (checks masked out) """ pdf_path = os.path.join(output_dir, "Concat_annotated.pdf") - ref_path = os.path.join(output_dir, "Reference.png") + # ref_path = os.path.join(output_dir, "Reference.png") + ref_path = os.path.join(output_dir, "Reference.jpg") json_path = os.path.join(output_dir, "checkboxes.json") if not (os.path.exists(pdf_path) and os.path.exists(ref_path)): @@ -39,9 +40,7 @@ def detect_checks_and_notes(output_dir): except Exception as e: print(f"Error reading PDF: {e}") return [], None - # print("Debug : user_pages", len(user_pages)) # Concatenate PDF pages back to one image if user saved as multiple pages - # (Xournal++ might preserve the long format or split it) total_h = sum(p.height for p in user_pages) user_img = Image.new("RGB", (user_pages[0].width, total_h)) y = 0 @@ -72,7 +71,7 @@ def detect_checks_and_notes(output_dir): DENSITY_THRESHOLD = 0.05 # 5% of pixels darkened # Mask to hide checkmarks from the "Notes" extraction - mask_img = Image.new("L", ref_img.size, 255) # White = keep, Black = hide + mask_img = Image.new("L", ref_img.size, 255) # White (255) = keep, Black (0) = hide mask_draw = ImageDraw.Draw(mask_img) for box in boxes: @@ -131,6 +130,12 @@ def detect_checks_and_notes(output_dir): notes.putalpha(Image.fromarray(final_alpha)) + # Debuging : Issues + # - The artifacts increase as we go lower in the image + # - Tous les rectangles sont présents + # - + notes.show() + return actions, notes from PIL import ImageDraw