How to Use Tesseract for OCR in Computer Vision
Use
pytesseract, a Python wrapper for Tesseract OCR, to extract text from images by loading the image and calling image_to_string(). This converts image content into editable text for computer vision tasks.Syntax
The basic syntax to use Tesseract OCR with Python is:
pytesseract.image_to_string(image): Extracts text from the given image.image: An image loaded using libraries likePILorOpenCV.- You can specify language and configuration options for better accuracy.
python
import pytesseract from PIL import Image # Load image image = Image.open('path_to_image.jpg') # Extract text text = pytesseract.image_to_string(image) print(text)
Example
This example shows how to load an image and extract text using Tesseract OCR with Python.
python
import pytesseract from PIL import Image # Load an example image image = Image.open('example_text.png') # Extract text from image extracted_text = pytesseract.image_to_string(image) print('Extracted Text:') print(extracted_text)
Output
Extracted Text:
Hello, this is a sample text from the image.
Common Pitfalls
Common mistakes when using Tesseract OCR include:
- Not installing Tesseract engine separately (it is not included with
pytesseract). - Using low-quality or noisy images which reduce accuracy.
- Forgetting to specify the correct language if the text is not English.
- Not preprocessing images (like converting to grayscale or thresholding) to improve OCR results.
python
import pytesseract from PIL import Image, ImageFilter # Wrong way: Using noisy image directly image = Image.open('noisy_image.jpg') text_wrong = pytesseract.image_to_string(image) # Right way: Preprocess image to improve OCR image_processed = image.convert('L').filter(ImageFilter.SHARPEN) text_right = pytesseract.image_to_string(image_processed) print('Without preprocessing:', text_wrong) print('With preprocessing:', text_right)
Output
Without preprocessing: H3ll0, th1s 1s n01sy t3xt.
With preprocessing: Hello, this is noisy text.
Quick Reference
| Function/Option | Description |
|---|---|
| pytesseract.image_to_string(image) | Extract text from image |
| Image.open(path) | Load image from file |
| image.convert('L') | Convert image to grayscale for preprocessing |
| --psm | Set page segmentation mode for Tesseract |
| lang='eng' | Specify language for OCR |
Key Takeaways
Install Tesseract OCR engine separately before using pytesseract in Python.
Load images with PIL or OpenCV before passing to pytesseract for text extraction.
Preprocess images (grayscale, sharpen) to improve OCR accuracy.
Specify language and page segmentation mode for better results.
Check and handle noisy or low-quality images carefully to avoid errors.