Registry / ai-ml / pytesseract

pytesseract

JSON →
library0.3.13pypypi✓ verified 27d ago

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 Pillow
INSTALL
IMPORT
SIG · PYTESSERACT
P
pytesseract
ai-mlpythonv0.3.13
Install
2.3s avg
Import
99ms
Disk
37MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.104s · 38.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.3s · import 0.094s · 39MB
37MB installed
● package 37MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytesseract
import pytesseract
The primary module for interacting with Tesseract.
Image
from PIL import Image
Used for opening and manipulating image files before passing them to pytesseract.

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.

import pytesseract from PIL import Image import os # Ensure Tesseract-OCR is installed and in your system's PATH. # If not, you may need to specify the path to the tesseract executable: # pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Example for Windows # pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract' # Example for macOS # Create a dummy image file for demonstration try: from PIL import ImageDraw, ImageFont img = Image.new('RGB', (200, 50), color = (255, 255, 255)) d = ImageDraw.Draw(img) fnt = ImageFont.truetype(os.environ.get('FONT_PATH', 'arial.ttf'), 20) # Use a common font or provide path d.text((10,10), "Hello World", font=fnt, fill=(0,0,0)) img.save('test_image.png') image_path = 'test_image.png' except ImportError: print("Pillow is missing drawing capabilities, cannot create test image. Please ensure you have a full Pillow install.") # Fallback for systems without font support (e.g., some CI environments) # This part would typically be replaced by loading an actual image file. image_path = os.environ.get('TEST_IMAGE_PATH', 'non_existent_image.png') # For testing without generating image if os.path.exists(image_path): try: text = pytesseract.image_to_string(Image.open(image_path)) print(f"Extracted text: {text.strip()}") except Exception as e: print(f"Error during OCR: {e}") print("Please ensure Tesseract-OCR is installed and configured correctly.") else: print(f"Test image '{image_path}' not found. Please provide one or install font for auto-generation.")
pytesseract --version
Debug
Known issues
breakingPython 2 and Python 3.5 support was dropped in version 0.3.7 due to End of Life for these Python versions. Python 3.6 support was dropped in version 0.3.9.
fix
Upgrade to Python 3.7 or newer. Python 3.8+ is officially supported.
affects: 0.3.7+, 0.3.9+
gotchaThe Tesseract-OCR engine (an external executable) must be installed separately and available in your system's PATH. `pytesseract` is only a wrapper.
fix
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.
affects: All versions
gotchaThe `Pillow` library is a mandatory dependency for image handling, but sometimes its installation can fail due to missing build dependencies (e.g., `libjpeg`, `zlib`).
fix
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.
affects: All versions
gotchaOCR processing can sometimes hang or take excessively long. Pytesseract can terminate processing after a timeout but will raise a `RuntimeError`.
fix
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.
affects: All versions
gotchaPerformance and accuracy of Tesseract OCR have significantly improved with versions 4 and 5 (which leverage neural networks) compared to older versions like 3.x.
fix
Ensure you are using Tesseract-OCR engine version 4.0 or higher for best results. Check your installed version with `tesseract --version`.
affects: < 4.0
Upgrade
Version history
0.3.13latest on PyPI · released Aug 16, 2024
Audit
Dependencies
Tesseract-OCR EnginerequiredPytesseract is a wrapper; the actual OCR engine must be installed separately on the system.
PillowrequiredRequired for image processing and handling various image formats.
Agent activity
15 hits · last 30 days
node
10
OpenAI (training)
1
Resources