small fixes

This commit is contained in:
2026-08-08 16:47:09 +02:00
parent f345f8bb4f
commit c535febbc4
6 changed files with 54 additions and 47 deletions
+18 -23
View File
@@ -3,6 +3,7 @@ from functools import lru_cache
import os
import time
import json # Added for schema output
import argparse
import tkinter as tk
from threading import Thread
from queue import Queue, Empty
@@ -14,10 +15,13 @@ DELIMITER_WIDTH = 5
DELIMITER_COLOR = (0, 0, 0)
OUTPUT_SIZE = (1800, 1000)
if len(sys.argv) < 2:
sys.exit("Usage: python script.py <directory_path_or_file_path>")
parser = argparse.ArgumentParser(description="PDF Cropper")
parser.add_argument("path", help="Directory path or PDF file path")
parser.add_argument("--fullpage", action="store_true", help="Process all files in full page mode (1 page per output file)")
args = parser.parse_args()
path_arg = sys.argv[1]
path_arg = args.path
fullpage_mode = args.fullpage
files = []
INPUT_DIR = ""
COPIES_DIR = ""
@@ -121,7 +125,6 @@ def process_single_pdf(filename, shift_offset=0, max_per_file=5):
Converts PDF to stitched images.
Returns a tuple: (preview_image_resized, list_of_split_images, schema_dict)
"""
# pdf_path = os.path.join(INPUT_DIR, filename)
try:
pages = get_pdf_pages(filename)
cropped_images = []
@@ -164,7 +167,6 @@ def process_single_pdf(filename, shift_offset=0, max_per_file=5):
# 3. Generate Preview (All stitched together, Resized)
full_stitch = stitch_images(cropped_images)
# preview_resized = full_stitch.resize(OUTPUT_SIZE, Image.LANCZOS)
preview_resized = full_stitch.resize(OUTPUT_SIZE, Image.BILINEAR)
schema = {
@@ -190,21 +192,15 @@ def save_results(result_tuple, filename):
# --- Cleanup: Delete existing files for this PDF ---
for f in os.listdir(OUTPUT_DIR):
file_path = os.path.join(OUTPUT_DIR, f)
# 1. Delete schema file
if f == f"{base_name}_schema.json":
os.remove(file_path)
# 2. Delete image files (pattern: basename_01.jpg, etc.)
elif f.startswith(f"{base_name}_") and f.endswith(".jpg"):
# Check if the suffix is strictly numeric (e.g. "01") to avoid
# deleting unrelated files like "file_v2_01.jpg" when processing "file.pdf"
suffix = f[len(base_name)+1:-4]
if suffix.isdigit():
os.remove(file_path)
# ---------------------------------------------------
# Save Images
for i, img in enumerate(splits):
# Suffix _01, _02, etc.
suffix = f"_{i+1:02d}"
output_filename = f"{base_name}{suffix}.jpg"
output_path = os.path.join(OUTPUT_DIR, output_filename)
@@ -217,14 +213,17 @@ def save_results(result_tuple, filename):
with open(json_path, 'w') as f:
json.dump(schema, f, indent=4)
print(f"Saved schema: {json_filename}")
# --- GUI Application ---
class ImageReviewer:
def __init__(self, file_list):
def __init__(self, file_list, default_max_per_file=5):
self.files = file_list
self.index = 0
self.current_shift = 0
self.current_max_per_file = 5
self.default_max_per_file = default_max_per_file
self.current_max_per_file = default_max_per_file
self.current_preview = None # Only stores the resized preview for GUI
self.is_processing = False
@@ -247,8 +246,7 @@ class ImageReviewer:
self.root.bind('n', lambda e: self.on_shift(50))
self.root.bind('N', lambda e: self.on_shift(100))
self.root.bind('t', lambda e: self.on_shift(-50))
self.root.bind('1', lambda e: self.on_set_max_pages(1)) # New Binding
self.root.bind('1', lambda e: self.on_set_max_pages(1))
# Start background pre-fetcher
self.bg_thread = Thread(target=self.prefetch_worker, daemon=True)
@@ -266,7 +264,6 @@ class ImageReviewer:
return
self.current_max_per_file = count
print(f"Setting max pages per file: {count}")
# Trigger reprocessing with current settings
self.trigger_processing(self.files[self.index], self.current_shift)
def prefetch_worker(self):
@@ -276,7 +273,7 @@ class ImageReviewer:
target = self.index + 1
if target < len(self.files) and target != idx_to_process:
fname = self.files[target]
get_pdf_pages(fname) # Just calling it warms the lru_cache
get_pdf_pages(fname)
idx_to_process = target
time.sleep(0.05)
@@ -290,7 +287,6 @@ class ImageReviewer:
self.is_processing = False
self.current_shift = 0
# Always trigger processing. If prefetched, get_pdf_pages returns instantly.
self.trigger_processing(filename, self.current_shift)
def trigger_processing(self, filename, shift):
@@ -317,7 +313,6 @@ class ImageReviewer:
self.load_current_image(use_prefetch=True)
self.is_processing = False
except Empty:
# Check again in 100ms
self.root.after(100, lambda: self.check_manual_queue(filename))
def handle_processing_result(self, result, filename):
@@ -325,7 +320,6 @@ class ImageReviewer:
preview, splits, schema = result
self.current_preview = preview
# Save in a background thread so the GUI updates instantly
Thread(target=save_results, args=(result, filename), daemon=True).start()
self.update_display(filename, schema)
@@ -350,7 +344,7 @@ class ImageReviewer:
def on_shift(self, amount):
if self.is_processing:
return # Ignore keys while processing
return
self.current_shift += amount
print(f"Applying shift: {self.current_shift}")
self.trigger_processing(self.files[self.index], self.current_shift)
@@ -360,12 +354,13 @@ class ImageReviewer:
return
self.index += 1
self.current_shift = 0
self.current_max_per_file = 5 # Reset to default
self.current_max_per_file = self.default_max_per_file
self.load_current_image(use_prefetch=True)
# --- Entry Point ---
if __name__ == "__main__":
if not files:
print("No PDF files found.")
else:
app = ImageReviewer(files)
app = ImageReviewer(files, default_max_per_file=1 if fullpage_mode else 5)