Prompting.py ; Miscs for Interro29
This commit is contained in:
@@ -24,15 +24,20 @@ def get_extra_pdfs_as_images(root_dir, label, annotating_module):
|
||||
return extra_images
|
||||
|
||||
def save_paginated_pdf(image_groups, output_path):
|
||||
"""Concatenates groups of images vertically, adding specific inner borders."""
|
||||
"""Concatenates groups of images vertically, adding inner borders and margins."""
|
||||
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)
|
||||
max_page_h = int(max_w * 1.414 * 1.25)
|
||||
|
||||
# Calculate 0.2 cm in pixels at 100 DPI (0.2 / 2.54 inches * 100)
|
||||
# Calculate sizes in pixels at 100 DPI
|
||||
border_px = int((0.2 / 2.54) * 100)
|
||||
left_margin = int((0.3 / 2.54) * 100)
|
||||
tb_margin = int((0.2 / 2.54) * 100)
|
||||
|
||||
# Available height for images once top/bottom margins are added
|
||||
max_content_h = max_page_h - (2 * tb_margin)
|
||||
|
||||
pages = []
|
||||
current_page_imgs = []
|
||||
@@ -46,11 +51,10 @@ def save_paginated_pdf(image_groups, output_path):
|
||||
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
|
||||
img = img.copy()
|
||||
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,
|
||||
@@ -60,11 +64,12 @@ def save_paginated_pdf(image_groups, output_path):
|
||||
|
||||
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
|
||||
if current_page_imgs and (current_h + group_h > max_content_h):
|
||||
# Create page with margins included in dimensions
|
||||
page = Image.new("RGB", (max_w + left_margin, current_h + 2 * tb_margin), "white")
|
||||
y = tb_margin
|
||||
for c_img in current_page_imgs:
|
||||
page.paste(c_img, (0, y))
|
||||
page.paste(c_img, (left_margin, y))
|
||||
y += c_img.height
|
||||
pages.append(page)
|
||||
|
||||
@@ -75,10 +80,10 @@ def save_paginated_pdf(image_groups, output_path):
|
||||
current_h += group_h
|
||||
|
||||
if current_page_imgs:
|
||||
page = Image.new("RGB", (max_w, current_h), "white")
|
||||
y = 0
|
||||
page = Image.new("RGB", (max_w + left_margin, current_h + 2 * tb_margin), "white")
|
||||
y = tb_margin
|
||||
for c_img in current_page_imgs:
|
||||
page.paste(c_img, (0, y))
|
||||
page.paste(c_img, (left_margin, y))
|
||||
y += c_img.height
|
||||
pages.append(page)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user