"""OpenCV preprocessing helpers for OCR on low-quality scans."""

from __future__ import annotations


def preprocess_image_for_ocr(image):
    """Enhance a PIL image before Tesseract OCR (denoise + adaptive threshold)."""
    try:
        import cv2
        import numpy as np
    except ImportError:
        return image

    array = np.array(image)
    if array.ndim == 3:
        gray = cv2.cvtColor(array, cv2.COLOR_RGB2GRAY)
    else:
        gray = array

    denoised = cv2.fastNlMeansDenoising(gray, h=10)
    binary = cv2.adaptiveThreshold(
        denoised,
        255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        cv2.THRESH_BINARY,
        31,
        11,
    )

    from PIL import Image

    return Image.fromarray(binary)
