Skip to content

Basic OCR with Tesseract

Abstract

Basic OCR with Tesseract is a Python project that extracts text from images using Tesseract OCR. It demonstrates image processing, external tool integration, and automation. This project is ideal for learning about OCR, file handling, and real-world automation.

Prerequisites

  • Python 3.6 or above
  • pytesseract (pip install pytesseractpip install pytesseract)
  • Pillow (pip install pillowpip install pillow)
  • Tesseract installed on your system

Before you Start

Install Python, Tesseract, pytesseract, and Pillow. Ensure Tesseract is in your system PATH.

Getting Started

  1. Create a folder named ocr-tesseractocr-tesseract.
  2. Create a file named basic_ocr_with_tesseract.pybasic_ocr_with_tesseract.py.
  3. Copy the code below into your file.
⚙️ Basic OCR with Tesseract
Basic OCR with Tesseract
"""
Basic OCR with Tesseract
 
A Python application that uses Tesseract OCR to extract text from images. Features include:
- GUI for uploading images
- Displaying extracted text
"""
 
import pytesseract
from PIL import Image
from tkinter import Tk, Label, Button, filedialog, Text, Scrollbar, END
 
 
class BasicOCR:
    def __init__(self, root):
        self.root = root
        self.root.title("Basic OCR with Tesseract")
 
        self.label = Label(root, text="Upload an image to extract text:")
        self.label.pack(pady=10)
 
        self.upload_button = Button(root, text="Upload Image", command=self.upload_image)
        self.upload_button.pack(pady=5)
 
        self.text_area = Text(root, wrap="word", width=60, height=20)
        self.text_area.pack(pady=10)
 
        self.scrollbar = Scrollbar(root, command=self.text_area.yview)
        self.scrollbar.pack(side="right", fill="y")
        self.text_area.config(yscrollcommand=self.scrollbar.set)
 
    def upload_image(self):
        """Open a file dialog to upload an image."""
        file_path = filedialog.askopenfilename(
            filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.tiff")]
        )
        if file_path:
            self.extract_text(file_path)
 
    def extract_text(self, file_path):
        """Extract text from the uploaded image using Tesseract OCR."""
        try:
            image = Image.open(file_path)
            extracted_text = pytesseract.image_to_string(image)
            self.text_area.delete(1.0, END)
            self.text_area.insert(END, extracted_text)
        except Exception as e:
            self.text_area.delete(1.0, END)
            self.text_area.insert(END, f"Error: {e}")
 
 
def main():
    root = Tk()
    app = BasicOCR(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
Basic OCR with Tesseract
"""
Basic OCR with Tesseract
 
A Python application that uses Tesseract OCR to extract text from images. Features include:
- GUI for uploading images
- Displaying extracted text
"""
 
import pytesseract
from PIL import Image
from tkinter import Tk, Label, Button, filedialog, Text, Scrollbar, END
 
 
class BasicOCR:
    def __init__(self, root):
        self.root = root
        self.root.title("Basic OCR with Tesseract")
 
        self.label = Label(root, text="Upload an image to extract text:")
        self.label.pack(pady=10)
 
        self.upload_button = Button(root, text="Upload Image", command=self.upload_image)
        self.upload_button.pack(pady=5)
 
        self.text_area = Text(root, wrap="word", width=60, height=20)
        self.text_area.pack(pady=10)
 
        self.scrollbar = Scrollbar(root, command=self.text_area.yview)
        self.scrollbar.pack(side="right", fill="y")
        self.text_area.config(yscrollcommand=self.scrollbar.set)
 
    def upload_image(self):
        """Open a file dialog to upload an image."""
        file_path = filedialog.askopenfilename(
            filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.tiff")]
        )
        if file_path:
            self.extract_text(file_path)
 
    def extract_text(self, file_path):
        """Extract text from the uploaded image using Tesseract OCR."""
        try:
            image = Image.open(file_path)
            extracted_text = pytesseract.image_to_string(image)
            self.text_area.delete(1.0, END)
            self.text_area.insert(END, extracted_text)
        except Exception as e:
            self.text_area.delete(1.0, END)
            self.text_area.insert(END, f"Error: {e}")
 
 
def main():
    root = Tk()
    app = BasicOCR(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
  1. Run the script: python basic_ocr_with_tesseract.pypython basic_ocr_with_tesseract.py

Explanation

Code Breakdown

  1. Import modules
from PIL import Image
import pytesseract
from PIL import Image
import pytesseract
  1. Load image and extract text
img = Image.open('sample.png')
text = pytesseract.image_to_string(img)
print(text)
img = Image.open('sample.png')
text = pytesseract.image_to_string(img)
print(text)

Features

  • Extracts text from images
  • Uses Tesseract OCR engine
  • Simple CLI usage
  • Easy to extend for more features

How It Works

  • Loads image file
  • Passes to Tesseract for OCR
  • Prints extracted text

Use Cases

  • Digitize documents
  • Automate data entry
  • Learn OCR basics

Next Steps

You can enhance this project by:

  • Processing multiple images
  • Saving text to files
  • Adding a GUI
  • Supporting different languages

Enhanced Version Ideas

def process_folder():
    # OCR all images in a folder
    pass
 
def add_language_support():
    # Use Tesseract for other languages
    pass
def process_folder():
    # OCR all images in a folder
    pass
 
def add_language_support():
    # Use Tesseract for other languages
    pass

Troubleshooting Tips

  • Tesseract not found: Check installation and PATH
  • No text extracted: Try different images

Conclusion

This project teaches OCR, image processing, and automation. Extend it for more robust text extraction and features.

Was this page helpful?

Let us know how we did