Pytesseract OCR Python | Extracting Text From Images
Pytesseract OCR Python | Extracting Text From Images
Introduction
As a developer, you may need to Extract Text From Images. We can write a Python program to extract such textual information from each and every image. In Python, we can use the Pytesseract package for this OCR(Optical Character Recognition) process.
Adding Libraries
To get started with the Python script, we need to install a few required libraries.
Pytesseract
To install pytesseract, run the following command:
pip install pytesseract
Pillow
Pillow library acts as an image interpreter with all image processing capabilities.
To install the pillow, run the following command:
pip install pillow
Opencv-python
We will use OpenCV to recognize texts from the media files (images).
To install OpenCV-python, run the following command:
pip install opencv-python
Creating a Python tesseract script to Read Text From Images
Importing Libraries
Weāre almost ready to read text from images. Before that, though, you need to import the Pytesseract, Pillow, OpenCV, and Numpy libraries for extracting text from image python
# Import libraries
from PIL import Image
import pytesseract
import cv2
import numpy as np
from pytesseract import Output
import os
The following script specifies the path for the Tesseract engine executable file we installed earlier. If you installed Tesseract OCR in a different location, you need to update your path accordingly.
pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
Extract text from images
# Simply extracting text from image
image = Image.open("A:/Freelance/Projects/demo.jpg")
image = image.resize((300,150))
custom_config = r'-l eng --oem 3 --psm 6'
text = pytesseract.image_to_string(image,config=custom_config)
print(text)
The first step in reading text from an image is to open the image. You can do so by using the open() method of the Pillow libraryās Image object. To read the text from an image, first, pass the image object you just opened to the Pytesseract moduleās image to string() method. This is a pytesseract image to string article. The image to string() method converts the image text into a Python string, which you can then use however you like. Using the print() method, weāll simply print the string to our screen. To read the text from the car license plate image, run the script below.
Note: Youāll need to update the path of the image to match the location of the image you want to convert to string.
custom_config = r'-l eng --oem 3 --psm 6'
Here in the custom configuration you can see the āengā which indicates the English language i.e it will recognize the English letters you can also add multiple languages and āPSMā means Page segmentation which set the configuration of how the chunks will recognize the characters and āOEMā is the default configuration.
filename = "A:/demo.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write(text)
And to save that text, we create a file called demo. Every word from our image is saved in this file, so we can use it elsewhere in extracting text from image python.
Now, letās test our code with the image below as input.

Complete code for extracting text from image python
from PIL import Image
import pytesseract
import cv2
import numpy as np
from pytesseract import Output
import os
#Specifies PATH of Tesseract
pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
# Simply extracting text from image
image = Image.open("A:/Freelance/Projects/demo.jpg")
image = image.resize((300,150))
custom_config = r'-l eng --oem 3 --psm 6'
text = pytesseract.image_to_string(image,config=custom_config)
print(text)
#To Save The Text in Text file
filename = "A:/demo.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write(text)
Output:

The output below shows that the characters from the Passage have been correctly read. However, in addition, ā this symbol is also printed in the output.
The output shows that though Tesseract OCR can read text from an image, it is not 100% correct. An accuracy rate of less than 100% is typical with all OCR engines, so donāt let this discourage you.
You can search this(OpenCV put text) on google to know how pythonās OpenCV library can convert images to text
References - ocr-basics
Setting up Tesseract
references - opencv installtions
OCR (Optical Character Recognition) is a technique that recognizes or Extracts Text From Images. It may be used to transform handwritten or printed texts that are difficult to read into machine-readable.
You must install and set up tesseract on your PC before you can use OCR.
Install and Run Tesseract for Windows in 4 Easy Steps
Step-1. First, download the Tesseract OCR executables here.
- Once you open the executable file, youāll have to first select a language.

Click the āNextā button on the following dialog box.
Step-2. Configure Installation
- Installer Language

Click the āokā button on the following dialog box.
- License Agreement

Youāll be presented with a license agreement, as shown below. Click the āI Agreeā button if you agree to the terms.
- Tesseract OCR Setup





Step-3. Add Tesseract OCR for Windows Installation Directory to Environment Variables




Step-4. After the operation is completed, use the tesseract -v command to ensure that the OCR is installed

Conclusion
We began by learning how to install tesseract, a text extraction program. Then we grabbed a picture and retrieved the text from it and stored it in a text file. We discovered that in order to extract text from complicated pictures, we must employ OpenCVās image modification methods. I hope you enjoyed this tutorial and found it helpful.
References - Guide
Comments
Post a Comment