from __future__ import annotations import os import pytesseract def configure_tesseract() -> None: cmd = os.environ.get("TESSERACT_CMD", "").strip() if cmd: pytesseract.pytesseract.tesseract_cmd = cmd return if os.name != "nt": return # on Linux/macOS, tesseract should be in PATH common = [ r"C:\Program Files\Tesseract-OCR\tesseract.exe", r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe", ] for p in common: if os.path.exists(p): pytesseract.pytesseract.tesseract_cmd = p return def ocr_pil_image(img, lang: str = "mal", psm: int = 6, oem: int = 1) -> str: """ psm: 3 = fully automatic page segmentation 4 = assume single column of text 6 = assume a uniform block of text (often best for docs) 11 = sparse text oem: 1 = LSTM engine """ config = f"--oem {oem} --psm {psm}" text = pytesseract.image_to_string(img, lang=lang, config=config) return (text or "").strip()