Allow Gemini to report insufficient crop, and deal with it
This commit is contained in:
1 parent
247b124754
commit
ad7813ea8f
4 files changed
+215
-32
No files matched your search
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import threading
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox
|
||||
@@ -19,6 +20,7 @@ from copienator import (
|
||||
EvaluationWorkspace,
|
||||
ExitCode,
|
||||
atomic_write_json,
|
||||
read_json,
|
||||
execute,
|
||||
target_parser,
|
||||
workspace_from_target,
|
||||
@@ -221,6 +223,42 @@ def save_results(
|
||||
print(f"Saved schema: {base_name}_schema.json")
|
||||
|
||||
|
||||
def regenerate_full_page_image(workspace: EvaluationWorkspace, image_path: Path) -> None:
|
||||
"""Replace one strip montage with the same pages at their full width."""
|
||||
match = re.fullmatch(r"(.+)_(\d+)", image_path.stem)
|
||||
if not match or image_path.parent != workspace.cutleft_dir:
|
||||
raise ValueError(f"Cannot map Cutleft image to PDF pages: {image_path}")
|
||||
stem, part = match.group(1), int(match.group(2)) - 1
|
||||
schema_path = workspace.cutleft_dir / f"{stem}_schema.json"
|
||||
schema = read_json(schema_path)
|
||||
counts = schema["columns_per_file"]
|
||||
if (not isinstance(counts, list) or not counts
|
||||
or any(type(count) is not int or count < 1 for count in counts)
|
||||
or not 0 <= part < len(counts)):
|
||||
raise ValueError("Invalid Cutleft page distribution")
|
||||
first = sum(counts[:part])
|
||||
last = first + counts[part]
|
||||
page_numbers = set(range(first + 1, last + 1))
|
||||
if page_numbers <= set(schema.get("full_width_pages", [])):
|
||||
raise ValueError("Cutleft image already contains full pages")
|
||||
pages = get_pdf_pages(workspace.copies_dir / f"{stem}.pdf")
|
||||
if len(pages) != sum(counts):
|
||||
raise ValueError("Cutleft schema does not match the PDF page count")
|
||||
combined = stitch_images(pages[first:last])
|
||||
if combined is None:
|
||||
raise ValueError("No pages available for full-page fallback")
|
||||
crops = {crop["page"]: crop for crop in schema.get("page_crops", [])}
|
||||
for index in range(first, last):
|
||||
width, height = pages[index].size
|
||||
crops[index + 1] = {"page": index + 1, "left_px": 0, "right_px": width,
|
||||
"width_px": width, "height_px": height}
|
||||
schema["page_crops"] = [crops[key] for key in sorted(crops)]
|
||||
schema["full_width_pages"] = sorted(set(schema.get("full_width_pages", [])) | page_numbers)
|
||||
with staged_files(workspace.cutleft_dir) as staging:
|
||||
combined.save(staging / image_path.name, "JPEG", quality=95)
|
||||
atomic_write_json(staging / schema_path.name, schema)
|
||||
|
||||
|
||||
class ImageReviewer:
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
Reference in new issue
Block a user