Pytesseract is a Python wrapper for Google's Tesseract-OCR Engine, enabling optical character recognition (OCR) to 'read' and extract text from images. It supports various image types via Pillow and Leptonica libraries. The project is actively maintained, with frequent releases addressing bug fixes and adding new functionality, currently at version 0.3.15.
pip install pytesseract PillowVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to extract text from an image using `pytesseract.image_to_string`. It requires the Tesseract-OCR engine to be installed and accessible. It also includes a robust way to create a dummy image if needed, for demonstration purposes.
Upgrade to Python 3.7 or newer. Python 3.8+ is officially supported.
Install Tesseract-OCR via your OS package manager (e.g., `apt`, `brew`) or a Windows installer. If `tesseract` is not in PATH, specify its full path using `pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'` in your Python script.
Ensure you have the necessary system libraries for Pillow before running `pip install Pillow`. Consult Pillow's documentation for OS-specific prerequisites. Pytesseract v0.3.9 added build system requirements to help `pip` properly install Pillow.
Use the `timeout` parameter with `image_to_string` (e.g., `pytesseract.image_to_string(image, timeout=5)`) to set a maximum duration in seconds for OCR processing. Handle the `RuntimeError` if the timeout is exceeded.
Ensure you are using Tesseract-OCR engine version 4.0 or higher for best results. Check your installed version with `tesseract --version`.