Try img2pdf to generate the pdf

Issue : Xournal can't edit ??
master
Sébastien Miquel 2026-02-10 07:10:49 +01:00
parent 7656ca652f
commit 387064db8c
2 changed files with 21 additions and 6 deletions

View File

@ -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__":

View File

@ -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