Automatic orientation issues detection and fix, experimental
This commit is contained in:
1 parent
78a7b7679c
commit
46845c4612
12 files changed
+683
-9
No files matched your search
@@ -26,7 +26,8 @@ from copienator import (
|
||||
workspace_from_target,
|
||||
)
|
||||
from copienator.platform import launch_pdf_arranger
|
||||
from copienator.copy_errors import marked_copy_paths
|
||||
from copienator.copy_errors import marked_copy_paths, clear_copy_error
|
||||
from copienator.orientation import save_provenance, report_path
|
||||
|
||||
# Keep new shortcuts available with older personal configuration files.
|
||||
PAGE_SPLITTER_KB = {
|
||||
@@ -122,6 +123,7 @@ class PDFPreviewer:
|
||||
self.processing = False # Flag to prevent multiple finish calls
|
||||
try:
|
||||
self.doc = pymupdf.open(self.pdf_path)
|
||||
self.original_page_ids = {page.xref: index + 1 for index, page in enumerate(self.doc)}
|
||||
except (OSError, RuntimeError, ValueError) as e:
|
||||
self.failed = True
|
||||
self._temporary_directory.cleanup()
|
||||
@@ -325,6 +327,7 @@ class PDFPreviewer:
|
||||
# Re-open the file from disk to reset changes (like moved pages)
|
||||
try:
|
||||
self.doc = pymupdf.open(self.pdf_path)
|
||||
self.original_page_ids = {page.xref: index + 1 for index, page in enumerate(self.doc)}
|
||||
except (OSError, RuntimeError, ValueError) as e:
|
||||
messagebox.showerror("Error", f"Failed to reopen PDF file: {e}")
|
||||
self.master.destroy()
|
||||
@@ -446,7 +449,10 @@ class PDFPreviewer:
|
||||
self.split_pdf()
|
||||
self.reorder_pdfs()
|
||||
self.concate_files()
|
||||
commit_processed_pdf(self.workspace, self.pdf_path, self.final_file)
|
||||
output = commit_processed_pdf(self.workspace, self.pdf_path, self.final_file)
|
||||
save_provenance(self.workspace, output, self.output_origins)
|
||||
clear_copy_error(self.workspace, output, source='orientation')
|
||||
report_path(self.workspace, output.stem).unlink(missing_ok=True)
|
||||
except Exception as exc: # noqa: BLE001 - interactive boundary
|
||||
self.failed = True
|
||||
self.processing = False
|
||||
@@ -505,8 +511,10 @@ class PDFPreviewer:
|
||||
print("Starting PDF processing...")
|
||||
self.clean_up_dir(self.split_dir)
|
||||
|
||||
self.fragment_origins = {}
|
||||
for i, settings in enumerate(self.page_settings):
|
||||
page = self.doc.load_page(i)
|
||||
origin = getattr(self, 'original_page_ids', {}).get(page.xref, i + 1)
|
||||
line_x = settings['line_x']
|
||||
rotation_settings = settings['rotation']
|
||||
keep = settings['keep']
|
||||
@@ -530,12 +538,15 @@ class PDFPreviewer:
|
||||
boundaries = [left, *sorted({x for x in cuts if left < x < right}), right]
|
||||
# Each side is a multi-page PDF, keeping its subpages together
|
||||
# when the existing booklet ordering moves this side.
|
||||
origins = []
|
||||
with pymupdf.open() as output:
|
||||
for x0, x1 in zip(boundaries, boundaries[1:]):
|
||||
clip = pymupdf.Rect(x0, 0, x1, height)
|
||||
target = output.new_page(width=clip.width, height=clip.height)
|
||||
target.show_pdf_page(target.rect, self.doc, i, clip=clip)
|
||||
origins.append({'origin': origin, 'crop_points': list(clip)})
|
||||
output.save(output_path)
|
||||
self.fragment_origins[str(output_path)] = origins
|
||||
|
||||
self.doc.close()
|
||||
print(f"\nProcessing complete. Files are in '{self.split_dir}' directory.")
|
||||
@@ -550,6 +561,8 @@ class PDFPreviewer:
|
||||
def copy_side(source, destination):
|
||||
if os.path.isfile(source):
|
||||
shutil.copy2(source, destination)
|
||||
if hasattr(self, 'fragment_origins'):
|
||||
self.fragment_origins[str(destination)] = self.fragment_origins[str(source)]
|
||||
|
||||
while i < len(ps):
|
||||
psk = ps[i]['keep']
|
||||
@@ -630,7 +643,9 @@ class PDFPreviewer:
|
||||
key=natural_key
|
||||
)
|
||||
|
||||
self.output_origins = []
|
||||
for pdf in pdf_files:
|
||||
self.output_origins.extend(getattr(self, 'fragment_origins', {}).get(str(pdf), []))
|
||||
reader = PdfReader(pdf)
|
||||
for page in reader.pages:
|
||||
writer.add_page(page)
|
||||
|
||||
Reference in new issue
Block a user