Join our Discord Server
Tanvir Kour Tanvir Kour is a passionate technical blogger and open source enthusiast. She is a graduate in Computer Science and Engineering and has 4 years of experience in providing IT solutions. She is well-versed with Linux, Docker and Cloud-Native application. You can connect to her via Twitter https://x.com/tanvirkour

Best Way to Get Text from Image with Python

4 min read

Images not only save memorable moments but also text. For example, digital receipts, screenshots, or quotations with plain backgrounds.

But how can you access or copy the text given on images? While there are tools to extract text from an image, Python can also be used for this purpose. Yes, you heard right. And this solution is especially enjoyable for those who love coding.

So, what is the best method to use Python to extract text from images? Is it easier to do? What are the steps involved in it? Read this guide to learn!

Best Way to Extract Text from Image via Python

Python comes with a rich ecosystem of libraries for different purposes. And when it comes to extracting text from images, Python offers many libraries, such as:

  1. Tesseract (pytesseract)
  2. EasyOCR
  3. Keras-OCR
  4. TrOCR
  5. docTR

So, what is the best library to use? All are best since each can perform the task they are created for—extract text from the images.

However, the most commonly used library is Tesseract (combined with OpenCV), which we’ll use in this tutorial.

How to Fetch Text from An Image via Python? Step-by-Step Guide

The following are the steps to use Python to extract text from an image:

Install Python

The first step is to install Python in your system—the Python 3.6+ version is required for this purpose, as only these can use pytesseract.

Simply visit the official website, Python.org, hover on the Downloads option, and download the latest Python version based on your operating system.

NOTE: At the time this guide is being written, the latest version is Python 3.13.0, which we’ll use in the tutorial on the Windows operating system. If you find the latest version or have a different operating system, you can use that, too.

Once the .exe file is downloaded, click on it to start the installation process.

NOTE: Make sure you checkmark Add python.exe to PATH to automatically add Python to your system path so you can use Python anywhere on your system. If you forget, manually configure the system path as in this guide.

Install Tesseract OCR

Once Python is installed on your system, it is time to install the Tesseract OCR application.

To do this, visit this page: https://github.com/UB-Mannheim/tesseract/wiki. Download the latest 64-bit installer; click on the 1st URL from the page (see the image attached below).

Click the downloaded file and let the package load. Wait until the process is complete. Then, follow the installation process like this:

  1. Select the desired languages.
  2. Click Next.
  3. Click the “I Agree” button to accept all the terms and conditions.
  4. Choose “Install just for me” from the Choose Users section.

  1. Click Next from the Choose Components tab.
  2. Copy the installation location and click Next to proceed further.

  1. Click Next from the next window.
  2. Click Finish at the end to complete the installation process.

NOTE: Paste and save the installation location you copied to somewhere on your system (such as a TXT file); you will need this location later.

Install Pytesseract

Open the editor, create a new terminal, and install the pytesseract package.

Simply type this code into the terminal:

pip install pytesseract

After that, type this code into the terminal:

cd .\Desktop\

cd ‘\python apps\’

cd .\pytesseract\

Then, import pytesseract and name it as you want to. NOTE: We named it PYT.

For this, enter this command into the editor:

Important pytesseract as pyt

Install OpenCV

Once Pytesseract is installed, it is time to install the OpenCV package.

For this, go to the terminal section and type this down into it:

pip install opencv-python

Once the OpenCV is installed, go to the editor and import it with this line of code:

import cv2

Read Image Via OpenCV and Extract Text Via Pytesseract

Imported all the libraries? Good, very good. It is time to proceed further and use OpenCV to read the image and then extract text via the Pytesseract Image to Text function.

First, assign the file location to a name (such as text_image) and then use the “imread” function to read the image.

text_image = Image.open(image_path) #passed the image location

img = cv2.imread(“text_imgage”) #read the image via “imread” function

​Next, pass the Tesseract executables location which you have copied somewhere to the pytesseract module. We need to do this for the package to use the executable for reading and converting image into text.​

pyt.pytesseract.tesseract_cmd = “C:\\Users\\steve\\AppData\\Local\\Tesseract-OCR\\tesseract.exe”

text = pyt.image_to_string(img) #the object is passed to image to string function

print(text) #will read the image and return the text

The whole piece of code looks like this:

import pytesseract as pyt

import cv2

text_image = Image.open(image_path)

img = cv2.imread(“text_imgage”)

pyt.pytesseract.tesseract_cmd = “C:\\Users\\steve\\AppData\\Local\\Tesseract-OCR\\tesseract.exe”

text = pyt.image_to_string(img)

print(text)

This is how it looks like on the editor:

To print the text in the Terminal, use the line:

python .\pytesseract_basic.py

Hurrah! We have successfully converted the image into a text.

Save the Extracted Text in a TXT File (Optional)

If you have followed this guide so far, you would have extracted the text in the terminal window of the editor. However, if you want to save it on your system in a TXT file, use few lines of code:

with open (“text_file.txt”, “w+”) as f:

f.write (text)

NOTE: You’ll find the TXT file (with text) in the pytesseract folder you created on the system.

OCR Tool — No-Code Way to Get Text from Images

Other than coding manually in Python to extract text from images, several Online OCR Tools are there that use Python. You can use such tools to see the working and to observe how effectively Python is extracting text from images.

OCR tools (also called image to text converters) use Optical Character Recognition (OCR) technology to locate, recognize, and extract text from images.

Here is the catch: several OCR tools are available online. The real challenge isn’t using them, but finding the one that provides accurate results in no time.

To save your time, we suggest you use Imagetotext.info every time you want to do image-to-text conversion. Why? Because this tool uses Python libraries and is free to use, uses AI+OCR for best text extraction, and supports 24 languages and multiple image formats.

To use it or any other such tool, all you need to do is to paste the desired image into the image input box. You can also upload the image from the system.

Once the image is uploaded, click on the Convert button to start the text-extraction process.

This is how this tool extracts the text:

NOTE: Most of the OCR tools support batch processing. If you have multiple images, don’t forget to upload all in once and extract text from the all in one go.

Conclusion

Python is a programming language, and it can be used to get text from images.

There are two libraries that can help you with this: OpenCV and Tesseract. OpenCV processes the image and makes it clearer, while Tesseract extracts the text.

This approach works well for many types of documents or pictures with text. With just a few lines of code, you can automate the process and save both time and effort.

Have Queries? Join https://launchpass.com/collabnix

Tanvir Kour Tanvir Kour is a passionate technical blogger and open source enthusiast. She is a graduate in Computer Science and Engineering and has 4 years of experience in providing IT solutions. She is well-versed with Linux, Docker and Cloud-Native application. You can connect to her via Twitter https://x.com/tanvirkour

A Developer’s Guide to Mobile-First Shopify Store Design

As a developer, you know that mobile traffic dominates eCommerce. Imagine your clients’ customers scrolling through their phones, searching for products. What if the...
Tanvir Kour
5 min read

Leave a Reply

Join our Discord Server
Index