parent
7656ca652f
commit
387064db8c
|
|
@ -4,6 +4,7 @@ import json
|
||||||
import shutil
|
import shutil
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
import threading
|
import threading
|
||||||
|
import img2pdf
|
||||||
|
|
||||||
# Fix for Matplotlib in threads: Set backend to non-interactive 'Agg'
|
# Fix for Matplotlib in threads: Set backend to non-interactive 'Agg'
|
||||||
import matplotlib
|
import matplotlib
|
||||||
|
|
@ -224,8 +225,17 @@ def process_student(args):
|
||||||
with open(os.path.join(output_dir, "checkboxes.json"), "w") as f:
|
with open(os.path.join(output_dir, "checkboxes.json"), "w") as f:
|
||||||
json.dump(final_json_map, f, indent=2)
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,8 @@ def detect_checks_and_notes(output_dir):
|
||||||
notes_img: RGBA image of manual notes (checks masked out)
|
notes_img: RGBA image of manual notes (checks masked out)
|
||||||
"""
|
"""
|
||||||
pdf_path = os.path.join(output_dir, "Concat_annotated.pdf")
|
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")
|
json_path = os.path.join(output_dir, "checkboxes.json")
|
||||||
|
|
||||||
if not (os.path.exists(pdf_path) and os.path.exists(ref_path)):
|
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:
|
except Exception as e:
|
||||||
print(f"Error reading PDF: {e}")
|
print(f"Error reading PDF: {e}")
|
||||||
return [], None
|
return [], None
|
||||||
# print("Debug : user_pages", len(user_pages))
|
|
||||||
# Concatenate PDF pages back to one image if user saved as multiple 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)
|
total_h = sum(p.height for p in user_pages)
|
||||||
user_img = Image.new("RGB", (user_pages[0].width, total_h))
|
user_img = Image.new("RGB", (user_pages[0].width, total_h))
|
||||||
y = 0
|
y = 0
|
||||||
|
|
@ -72,7 +71,7 @@ def detect_checks_and_notes(output_dir):
|
||||||
DENSITY_THRESHOLD = 0.05 # 5% of pixels darkened
|
DENSITY_THRESHOLD = 0.05 # 5% of pixels darkened
|
||||||
|
|
||||||
# Mask to hide checkmarks from the "Notes" extraction
|
# 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)
|
mask_draw = ImageDraw.Draw(mask_img)
|
||||||
|
|
||||||
for box in boxes:
|
for box in boxes:
|
||||||
|
|
@ -131,6 +130,12 @@ def detect_checks_and_notes(output_dir):
|
||||||
|
|
||||||
notes.putalpha(Image.fromarray(final_alpha))
|
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
|
return actions, notes
|
||||||
|
|
||||||
from PIL import ImageDraw
|
from PIL import ImageDraw
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue