How to Do OCR in Python: Simple Steps with pytesseract
To do OCR in Python, use the
pytesseract library which acts as a wrapper for Google's Tesseract OCR engine. Install Tesseract on your system, then use pytesseract.image_to_string() to extract text from images.Syntax
The basic syntax to perform OCR with pytesseract is:
pytesseract.image_to_string(image): Extracts text from the given image.image: An image object loaded using libraries likePILorOpenCV.
You must have Tesseract OCR installed on your computer and accessible in your system path.
python
import pytesseract from PIL import Image # Load image image = Image.open('sample.png') # Extract text text = pytesseract.image_to_string(image) print(text)
Example
This example shows how to load an image and extract text using pytesseract. It demonstrates the full process from reading the image file to printing the recognized text.
python
import pytesseract from PIL import Image # Make sure Tesseract is installed and in your PATH # Load an example image image = Image.open('ocr_sample.png') # Perform OCR to extract text extracted_text = pytesseract.image_to_string(image) print('Extracted Text:') print(extracted_text)
Output
Extracted Text:
Hello, this is a sample text extracted from the image.
Common Pitfalls
Common mistakes when doing OCR in Python include:
- Not installing the Tesseract OCR engine separately (pytesseract is only a wrapper).
- Incorrect image path or unsupported image format.
- Low image quality or noisy images causing poor text extraction.
- Not specifying the correct language if text is not English.
Always check that Tesseract is installed and accessible, and preprocess images for better results.
python
import pytesseract from PIL import Image # Wrong: Using pytesseract without Tesseract installed # This will raise an error # Right: Ensure Tesseract is installed and in PATH # Example preprocessing for better OCR image = Image.open('ocr_sample.png').convert('L') # Convert to grayscale text = pytesseract.image_to_string(image) print(text)
Quick Reference
Tips for effective OCR in Python:
- Install Tesseract OCR from official repo.
- Use
pytesseractas a Python wrapper. - Preprocess images (grayscale, thresholding) to improve accuracy.
- Specify language with
langparameter if needed, e.g.,pytesseract.image_to_string(image, lang='eng'). - Use high-quality images with clear text.
Key Takeaways
Install Tesseract OCR engine separately before using pytesseract in Python.
Use pytesseract.image_to_string() to extract text from images easily.
Preprocess images to grayscale or clean noise for better OCR accuracy.
Specify the language parameter if your text is not in English.
Ensure image paths and formats are correct to avoid errors.