This Python script uses Tesseract OCR to recognize handwritten text from an image file. It is optimized for Windows and includes error handling for missing files and processing issues.
- Uses
pytesseract
andPillow
for OCR and image handling - Checks if the image file exists before processing
- Handles errors gracefully
- Prints recognized text to the console
- Python 3.x
- Tesseract OCR installed on Windows
- Python packages:
pillow
,pytesseract
- Install Python and Tesseract OCR.
- Install required Python packages:
pip install pillow pytesseract
- Set the correct path to Tesseract in the script if needed:
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
- Place your image file (e.g.,
handwritten_note.jpg
) in the same folder as the script.
Run the script in PowerShell:
python "text recognizer from photo.py"
π Recognized text:
<recognized text here>
from PIL import Image
import pytesseract
import os
# Set Tesseract path for Windows
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Use tessdata_best for handwritten models if available
custom_config = r'--oem 1 --psm 6 -l eng'
def recognize_handwritten(image_path):
if not os.path.exists(image_path):
print(f"β Image file not found: {image_path}")
return None
try:
img = Image.open(image_path)
text = pytesseract.image_to_string(img, config=custom_config)
return text
except Exception as e:
print(f"β Error processing image: {e}")
return None
# Example usage
image_file = "handwritten_note.jpg"
text = recognize_handwritten(image_file)
if text:
print("π Reconized text:\n", text)