0
0
Computer Visionml~5 mins

Tesseract OCR in Computer Vision

Choose your learning style9 modes available
Introduction

Tesseract OCR helps computers read text from pictures. It turns images with words into editable text.

You have a photo of a receipt and want to get the text to save or search.
You scanned a paper document and want to convert it into a text file.
You want to read text from signs or labels in images automatically.
You need to extract text from screenshots or photos for quick notes.
Syntax
Computer Vision
import pytesseract
from PIL import Image

text = pytesseract.image_to_string(Image.open('image.png'))
print(text)

Make sure Tesseract OCR software is installed on your computer.

Use pytesseract.image_to_string() to get text from an image.

Examples
Extract text from a receipt image.
Computer Vision
text = pytesseract.image_to_string(Image.open('receipt.jpg'))
Extract English text specifically from a document image.
Computer Vision
text = pytesseract.image_to_string(Image.open('document.png'), lang='eng')
Use page segmentation mode 6 for a uniform block of text like a sign.
Computer Vision
text = pytesseract.image_to_string(Image.open('sign.jpg'), config='--psm 6')
Sample Model

This code creates a simple image with the text 'Hello OCR' and uses Tesseract to read it back.

Computer Vision
import pytesseract
from PIL import Image

# Load an example image with text
image = Image.new('RGB', (200, 60), color=(255, 255, 255))

# Draw simple text on the image
from PIL import ImageDraw, ImageFont

draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
draw.text((10, 10), 'Hello OCR', font=font, fill=(0, 0, 0))

# Use Tesseract to extract text
text = pytesseract.image_to_string(image)
print('Extracted Text:', text.strip())
OutputSuccess
Important Notes

Tesseract works best with clear, high-contrast images.

You can improve accuracy by preprocessing images (e.g., converting to black and white).

Install Tesseract software separately; pytesseract is just a Python wrapper.

Summary

Tesseract OCR turns images with text into editable text.

Use pytesseract.image_to_string() to extract text from images.

Good image quality helps Tesseract read text accurately.