Sari la conținut

pyt_utils/image_preprocessing Routines Documentation

Overview

The image_preprocessing module provides high-level functions for automated image preprocessing, specifically designed for scanned document OCR optimization. It combines low-level OpenCV utilities with advanced algorithms for deskewing, zone detection, and cropping.

Core Functions

fix_image(image_path, enable_manual_correction=False, rotate=True, maximize_height=True, debug_draw=False, interactive_debug=False, debug=False)

Signature:

def fix_image(image_path: str, enable_manual_correction: bool = False, rotate: bool = True,
             maximize_height: bool = True, debug_draw: bool = False, interactive_debug: bool = False,
             debug: bool = False) -> Optional[np.ndarray]

Parameters: - image_path: Path to input image file (JPG, PNG) - enable_manual_correction: If True, launches interactive UI for manual adjustments - rotate: Whether to auto-detect and correct image rotation/skew - maximize_height: Whether to expand crop area to full image height - debug_draw: If True, returns overlay image showing detected text zones instead of processed image - interactive_debug: Shows intermediate results in pop-up windows - debug: Prints detailed processing information

Returns: - Processed OpenCV image array, or None if processing fails

Algorithm Flow: 1. Image Loading & Validation: Loads image, validates format 2. Double-Page Detection: Analyzes if image contains two scanned pages 3. Text Zone Detection: Finds optimal text region using projection-based analysis 4. Auto-Rotation: Detects and corrects text orientation 5. Cropping: Extracts text area with configurable height maximization 6. Manual Correction (Optional): Allows user adjustment via DearPyGui interface

cli_crop_page(filepath, output_path=None, no_rotate=False, debug_draw=False, interactive_debug=False, debug=False)

Signature:

def cli_crop_page(filepath: str, output_path: str = None, no_rotate: bool = False,
                 debug_draw: bool = False, interactive_debug: bool = False, debug: bool = False)

Purpose: Command-line interface wrapper for single image processing.

Parameters: - filepath: Input image path - output_path: Optional output path (auto-generated if None) - no_rotate: Skip rotation correction if True

Returns: Boolean success status

cli_fix_pdf(filepath, output_path=None, no_rotate=False, skip_intervals="", manual_intervals="", debug_draw=False, debug=False, interactive_debug=False)

Signature:

def cli_fix_pdf(filepath: str, output_path: str = None, no_rotate: bool = False,
               skip_intervals: str = "", manual_intervals: str = "",
               debug_draw: bool = False, debug: bool = False, interactive_debug: bool = False)

Purpose: Complete PDF processing pipeline for multiple pages.

Parameters: - filepath: Input PDF path - output_path: Optional output PDF path - skip_intervals: Page ranges to skip (e.g., "1-3,7,10-15") - manual_intervals: Pages requiring manual intervention

Process: 1. Extracts each PDF page as image 2. Applies fix_image to each page 3. Supports selective manual processing for specific pages 4. Reconstructs processed images into new PDF

Returns: Boolean success status

cli_ocr_pdf(filepath, output_path, split_pages=False, skip_intervals="", manual_intervals="", debug_output=False, debug=False)

Signature:

def cli_ocr_pdf(filepath: str, output_path: str, split_pages: bool = False,
               skip_intervals: str = "", manual_intervals: str = "",
               debug_output: bool = False, debug: bool = False)

Purpose: OCR processing with optional text extraction and confidence analysis.

Parameters: - filepath: Input PDF path - output_path: Output text file path - split_pages: Create separate file per page if True - debug_output: Include OCR confidence metrics in output

Features: - Tesseract-based OCR with adjustable PSM modes - Confidence scoring and error highlighting - Page-by-page processing with skip/manual options

Internal Functions

_parse_intervals(intervals_str)

Purpose: Parse page range strings into integer arrays.

Example: "1-3,7,10-15" → [0,1,2,6,9,10,11,12,13,14] (0-based indexing)

Dependencies

Required Packages: - OpenCV (cv2) - NumPy - PyTesseract - Fitz (PyMuPDF) - PIL (Pillow) - pdf2image - DearPyGui (optional, for manual correction)

Internal Dependencies: - All functions from pyt_utils.opencv_utils - Basic image processing primitives

Error Handling

All functions include comprehensive error handling: - File existence validation - Image format checking - OCR engine availability - UI components (optional) - Processing pipeline failures

Performance Notes

  • Uses efficient projection-based text detection
  • Minimizes unnecessary rotations via threshold checking
  • Supports batch processing for PDF operations
  • Memory-efficient for large documents (page-by-page processing)