Initial support for _F output file
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
||||
import collections
|
||||
import concurrent.futures
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageDraw
|
||||
import threading
|
||||
|
||||
import annotating
|
||||
@@ -12,6 +12,79 @@ import annotating
|
||||
from utils import natural_key
|
||||
from reading_annotations import detect_checks_and_notes, has_significant_notes
|
||||
|
||||
def get_extra_pdfs_as_images(root_dir, label, annotating_module):
|
||||
"""Fetches Text and Sol pdfs for a given label and converts them to images."""
|
||||
extra_images = []
|
||||
for folder in ["Text", "Sol"]:
|
||||
pdf_path = os.path.join(root_dir, folder, f"{label}.pdf")
|
||||
if os.path.exists(pdf_path):
|
||||
img, _, _ = annotating_module.make_base_image(pdf_path)
|
||||
if img:
|
||||
extra_images.append(img)
|
||||
return extra_images
|
||||
|
||||
def save_paginated_pdf(image_groups, output_path):
|
||||
"""Concatenates groups of images vertically, adding specific inner borders."""
|
||||
if not image_groups:
|
||||
return
|
||||
|
||||
max_w = max(img.width for group in image_groups for img in group)
|
||||
max_page_h = int(max_w * 1.414 * 1.3)
|
||||
|
||||
# Calculate 0.2 cm in pixels at 100 DPI (0.2 / 2.54 inches * 100)
|
||||
border_px = int((0.2 / 2.54) * 100)
|
||||
|
||||
pages = []
|
||||
current_page_imgs = []
|
||||
current_h = 0
|
||||
|
||||
for group in image_groups:
|
||||
if not group:
|
||||
continue
|
||||
|
||||
# Process the group to add borders
|
||||
processed_group = []
|
||||
for i, img in enumerate(group):
|
||||
if i in (0, 1):
|
||||
img = img.copy() # Do not modify the original image object in memory
|
||||
draw = ImageDraw.Draw(img)
|
||||
color = "black" if i == 0 else "blue"
|
||||
|
||||
# Draw the border inside the image edges
|
||||
draw.rectangle(
|
||||
[0, 0, img.width - 1, img.height - 1],
|
||||
outline=color,
|
||||
width=border_px
|
||||
)
|
||||
processed_group.append(img)
|
||||
|
||||
group_h = sum(img.height for img in processed_group)
|
||||
|
||||
if current_page_imgs and (current_h + group_h > max_page_h):
|
||||
page = Image.new("RGB", (max_w, current_h), "white")
|
||||
y = 0
|
||||
for c_img in current_page_imgs:
|
||||
page.paste(c_img, (0, y))
|
||||
y += c_img.height
|
||||
pages.append(page)
|
||||
|
||||
current_page_imgs = processed_group
|
||||
current_h = group_h
|
||||
else:
|
||||
current_page_imgs.extend(processed_group)
|
||||
current_h += group_h
|
||||
|
||||
if current_page_imgs:
|
||||
page = Image.new("RGB", (max_w, current_h), "white")
|
||||
y = 0
|
||||
for c_img in current_page_imgs:
|
||||
page.paste(c_img, (0, y))
|
||||
y += c_img.height
|
||||
pages.append(page)
|
||||
|
||||
if pages:
|
||||
pages[0].save(output_path, "PDF", resolution=100.0, save_all=True, append_images=pages[1:])
|
||||
|
||||
def apply_actions_and_regenerate_grouped(root_dir, data, student_id, actions, label_notes, all_labels):
|
||||
"""
|
||||
Modifies data based on actions, pastes label-specific note crops,
|
||||
@@ -136,8 +209,11 @@ def apply_actions_and_regenerate_grouped(root_dir, data, student_id, actions, la
|
||||
else:
|
||||
if len(result.get('feedback', [])) != 0:
|
||||
perfect_no_comment = False
|
||||
|
||||
if not perfect_no_comment:
|
||||
concat_list_F.append(final_img)
|
||||
extras = get_extra_pdfs_as_images(root_dir, label, annotating)
|
||||
extras.append(final_img)
|
||||
concat_list_F.append(extras)
|
||||
|
||||
# --- 3. Save Final Outputs ---
|
||||
with open(score_path, "w") as f:
|
||||
@@ -158,17 +234,21 @@ def apply_actions_and_regenerate_grouped(root_dir, data, student_id, actions, la
|
||||
logs.append(f" Saved regenerated Concat.jpg")
|
||||
|
||||
if concat_list_F:
|
||||
max_w = max(i.width for i in concat_list_F)
|
||||
total_h = sum(i.height for i in concat_list_F)
|
||||
full_img = Image.new("RGB", (max_w, total_h), "white")
|
||||
pdf_out_path = os.path.join(output_dir, "Concat_F.pdf")
|
||||
save_paginated_pdf(concat_list_F, pdf_out_path)
|
||||
logs.append(f" Saved regenerated Concat_F.pdf")
|
||||
|
||||
y = 0
|
||||
for img in concat_list_F:
|
||||
full_img.paste(img, (0, y))
|
||||
y += img.height
|
||||
# max_w = max(i.width for i in concat_list_F)
|
||||
# total_h = sum(i.height for i in concat_list_F)
|
||||
# full_img = Image.new("RGB", (max_w, total_h), "white")
|
||||
|
||||
full_img.save(os.path.join(output_dir, "Concat_F.jpg"))
|
||||
logs.append(f" Saved regenerated Concat_F.jpg")
|
||||
# y = 0
|
||||
# for img in concat_list_F:
|
||||
# full_img.paste(img, (0, y))
|
||||
# y += img.height
|
||||
|
||||
# full_img.save(os.path.join(output_dir, "Concat_F.jpg"))
|
||||
# logs.append(f" Saved regenerated Concat_F.jpg")
|
||||
|
||||
return "\n".join(logs)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user