Image to Text in Python

In this Post, We will see how to convert image to text in Python. Means How to Fetch Text written on images using Python.

There are different modules used for this purpose. We will disucss 2 of them.
1. Easyocr
2. Tesseract

Easyocr

EasyOCR, as the name suggests, is a Python package that allows computer vision developers to effortlessly perform Optical Character Recognition. When it comes to OCR, EasyOCR is by far the most straightforward way to apply Optical Character Recognition: The EasyOCR package can be installed with a single pip command.

pip install easyocr

Code

import easyocr
reader = easyocr.Reader(['en'])
# this needs to run only once to load the model into memory
result = reader.readtext('image.jpg')

Tesseract

Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and “read” the text embedded in images. Python-tesseract is a wrapper for Google’s Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types and fetch text from them. It can be installed using a single pip command

pip install tesseract

Here is the code to convert image to text in python using tessract. However some display image libraries has been used to show image in jupyter notebook.

import pytesseract as pt
#glob and IPython.display are used for display image in jupyter notebook
import glob
from IPython.display import Image, display
print(r'Enter path of Image e.g G:\images\1.jpg')
path = input()
display(Image(filename=path)) #it displays image in jupyter notebook
print(pt.image_to_string(path)) #it fetches txt from image and prints that.

Output

Related Post

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *