How to extract text from image using Python
In this post, you will learn how to extract text from an image using Python OpenCV and OCR.
OpenCV stands for Open Source Computer Vision Library. It is a free, open source library that is used for computer vision applications. This library is written in optimized C/C++ and is supported on Windows, Linux, Android, and MacOS. It is used in a wide range of applications like object identification, machine learning, face recognition, deep learning, mobile robotics, gesture recognition, and much more.
OCR (optical character recognition) is the process of recognizing text in an image and converting it into text. OCR has applications in a wide range of enterprises and capacities. Thus, everything from checking records, bank articulations, receipts, transcribed reports, coupons, handwritten documents, and so forth, all fall under the OCR. It is basically used to convert scanned documents into searchable text files. It works on optical recognition technology. The OCR module is designed with more advanced features for performing additional functionalities. This process can also reduce document file sizes for easier transfer and sharing. It also saves a lot of time by frequently transferring paper documents into electronic files.
Python-tesseract is an optical character recognition (OCR) tool for Python. It is an open-source text recognition engine. It is widely used to extract text from images or documents because it provides a more accurate result. The best part is that it supports an extensive variety of languages. This is extremely useful in situations where a lot of documentation is involved, like at educational institutes, news agencies, government workplaces, etc.
Install tesseract OCR on Windows
For Windows users, download the exe file of tesseract either 32-bit or 64-bit as per your system, from here. You can directly execute the downloaded exe file by following these steps-
https://github.com/UB-Mannheim/tesseract/wiki
And then we need to configure the Tesseract path in the System Variables window under the Environment Variables window.
Installing Modules
We need to install three modules- opencv-python, pytesseract, and tesseract. Pytesseract is a wrapper for the Tesseract-OCR Engine. It is also helpful as a stand-alone invocation script to tesseract. It is able to read all image types supported by the Pillow and Leptonica imaging libraries, including png, jpeg, gif, bmp, and others. We can install these using the pip tool.
pip install opencv-python
pip install pytesseract
pip install tesseract
Python Code to Extract Text From Image using Tesseract
Suppose we have the following test image located in the same working directory.
First, we have created a Python file and imported all the necessary modules at the top.
# text recognition
import cv2
import pytesseract
Next, we used the imread() function to load the test image from the specified location.
# read image
img = cv2.imread('quotes.jpg')
Here, we have set the configuration custom options.
# configurations
config = ('-l eng --oem 1 --psm 3')
If you have not configured the tesseract executable in your System variables PATH, include the following.
# pytessercat
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
Next, we convert from image to string using the method image_to_string().
text = pytesseract.image_to_string(img, config=config)
Finally, we can print the extracted text form Image.
# print text
text = text.split('\n')
print(text)
Complete Code: Extract text from image Python
Let's merge all the above code and execute it.
# text recognition
import cv2
import pytesseract
# read image
img = cv2.imread('quotes.jpg')
# configurations
config = ('-l eng --oem 1 --psm 3')
# pytessercat
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
text = pytesseract.image_to_string(img, config=config)
# print text
text = text.split('\n')
print(text)
The above code returns the following output-
The above dictionary has information about your input image. Tesseract is ideal for scanning clean documents, and we can easily convert the image's text from OCR to Word or to any other required format.
Extract an image and save it as a text file
In this, we first converted the image to grayscale and then specified the kernel shape and size. Next, we found the contours and looped over them, chopping the rectangle area. Next, we have passed the rectangle area onto pytesseract for extracting text from it and then writing it into the text file.
# import modules
import cv2
import pytesseract
# read image
img = cv2.imread('quotes.png')
# set configurations
config = ('-l eng --oem 1 --psm 3')
# Convert the image to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# OTSU threshold performing
ret, threshimg = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
# Specifying kernel size and structure shape.
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
# Appplying dilation on the threshold image
dilation = cv2.dilate(threshimg, rect_kernel, iterations = 1)
# getting contours
img_contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
# Loop over contours and crop and extract the text file
for cnt in img_contours:
x, y, w, h = cv2.boundingRect(cnt)
# Drawing a rectangle
rect = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Cropping the text block
cropped_img = img[y:y + h, x:x + w]
# Open the text file in append mode
file = open("recognized.txt", "a")
# Applying tesseract OCR on the cropped image
text = pytesseract.image_to_string(cropped_img)
# Appending the text into file
file.write(text)
file.write("\n")
# Close the file
file.close
Output of the above code-
Related Articles
OpenCV Logical Operators- Bitwise AND, OR, NOR, XORArithmetic Operations on Images using Python OpenCV
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Python OpenCV Erosion and Dilation
Color detection OpenCV Python
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Body tracking OpenCV
Python Spell Checker Program
Python remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml