add overwrite argument to annotating with checks

master
Sébastien Miquel 2026-02-25 21:25:31 +01:00
parent 375305439a
commit 63489c95c7
1 changed files with 17 additions and 28 deletions

View File

@ -100,15 +100,21 @@ def natural_key(text):
def process_student(args): def process_student(args):
"""Thread worker: Processes one student.""" """Thread worker: Processes one student."""
root_dir, student_id, labels = args root_dir, student_id, labels, overwrite = args
print(f"Generating Checkable PDF for: {student_id}")
output_dir = os.path.join(root_dir, "Bnot", f"Copie{student_id}") output_dir = os.path.join(root_dir, "Bnot", f"Copie{student_id}")
if os.path.exists(output_dir): if os.path.exists(output_dir):
if not overwrite:
print(f"Skipping {student_id}: Output already exists.")
return
shutil.rmtree(output_dir) shutil.rmtree(output_dir)
print(f"Generating Checkable PDF for: {student_id}")
os.makedirs(output_dir) os.makedirs(output_dir)
label_images = [] label_images = []
# ... (rest of the function remains exactly the same)
all_checkboxes = [] all_checkboxes = []
bnote_entries = [] # For bnote.json bnote_entries = [] # For bnote.json
@ -178,17 +184,6 @@ 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)
# # Pour éviter du drift, avec img2pdf
# # Pb : Xournal can't add annotations
# 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))
# Avec reportlab.pdfgen
# Au moins, le drift n'empire pas au fil de la copie
temp_img_path = os.path.join(output_dir, "Reference.jpg") # Can't use png here temp_img_path = os.path.join(output_dir, "Reference.jpg") # Can't use png here
concat_img.save(temp_img_path, quality=90) concat_img.save(temp_img_path, quality=90)
@ -198,21 +193,17 @@ def process_student(args):
c.drawImage(temp_img_path, 0, 0, width=w, height=h) c.drawImage(temp_img_path, 0, 0, width=w, height=h)
c.save() c.save()
# print("Debug : size", w, h) import argparse # Added
# 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"))
# Try to fix the drift with 72 DPI, non essayé
# concat_img.save(os.path.join(output_dir, "Concat.pdf"), "PDF", resolution=72.0)
# concat_img.save(os.path.join(output_dir, "Reference.jpg"))
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 2: parser = argparse.ArgumentParser(description="Generate annotated PDFs.")
print("Usage: python annotating_with_checks.py <Dir> or <File>") parser.add_argument("input_path", help="Directory or specific file path")
sys.exit(1) parser.add_argument("--overwrite", action="store_true", help="Overwrite existing output files")
input_path = sys.argv[1] args = parser.parse_args()
input_path = args.input_path
overwrite = args.overwrite # Capture flag
target_id = None target_id = None
# Detect if input is a specific file # Detect if input is a specific file
@ -238,9 +229,7 @@ if __name__ == "__main__":
print(f"Student ID {target_id} not found in directory scan.") print(f"Student ID {target_id} not found in directory scan.")
results = {} results = {}
tasks = [(root_dir, sid, lbls) for sid, lbls in results.items()] tasks = sorted([(root_dir, sid, lbls, overwrite) for sid, lbls in results.items()])
# print(tasks)
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
results = executor.map(process_student, tasks) results = executor.map(process_student, tasks)