"""OCR extraction for images using pytesseract. Handles .jpg, .jpeg, .png files. Configured for German + English (deu+eng). Returns empty text (not error) when OCR cannot extract readable content. """ import logging from pathlib import Path from .base import ExtractionResult logger = logging.getLogger(__name__) SUPPORTED_EXTENSIONS = {".jpg", ".jpeg", ".png"} class OCRExtractor: """Extracts text from images using Tesseract OCR.""" def __init__(self, language: str = "deu+eng"): self.language = language def can_handle(self, file_path: Path) -> bool: """Return True for .jpg, .jpeg, .png files.""" return file_path.suffix.lower() in SUPPORTED_EXTENSIONS def extract(self, file_path: Path) -> ExtractionResult: """Run OCR on an image file. Returns empty text on failure.""" text = self._run_ocr(file_path) return ExtractionResult( text=text, source_file=file_path, extraction_method="ocr", metadata={"ocr_language": self.language}, ) def _run_ocr(self, file_path: Path) -> str: """Execute pytesseract on the image. Returns empty string on failure.""" try: import pytesseract from PIL import Image image = Image.open(file_path) text = pytesseract.image_to_string(image, lang=self.language) return text.strip() except Exception as e: logger.warning("OCR extraction failed for %s: %s", file_path, e) return ""