garryji/text-from-image

Public
0 runs

How to Extract Text from Image using Python?

The process of text extraction from images and documents is popular in different fields. Business, education, and writing are among the major fields that have to deal with image to text conversion.

People extract text from images to make it editable, shareable, searchable, and easily manageable. To do image to text conversion, Optical Character Recognition technology-based online tools are used.

Besides the online image to text extractors, this conversion is being done using Python. In this post, we shall comprehensively discuss the complete process of extracting text from images using Python.

Methods of Extracting Text from Images Using Python

Python is a well-known programming language that is used for developing as well as tuning different types of online apps and websites. Nowadays, it is used to carry out text extraction from images, using multiple methods.

The most common ways to use Python for text extraction from images are two that are:

  • Using Tesseract along with Python.
  • Using EasyOCR with Python.

Let’s discuss one of the data extraction methods using Python in the following sections.

Text Extraction from Images Using Tesseract With Python

Tesseract is a tool that is used along with Python to make it perform OCR. To use Tesseract with Python, we’ll use the Python-Tesseract OCR library which is also known as Pytesseract. Let’s elaborate on the process in the upcoming sections.

1st Step: Install Python On Your Device

The first step begins with downloading and installing Python’s version which is higher than the 3.5 version from its official website.

For Example:
You are going to install the 3.12 version of Python. Then you need to visit its installation website and select “Add Python 3.12 to PATH”. Doing so will add the required Python to your system.

2nd Step: Install the Tesseract tool

Now, you need to enable OCR technology to work with Python language on images or documents. For this, it is necessary to download the latest version of the Tesseract software.

After installing the Tesseract, you are required to open the “CLI Window”. From the window, you should navigate to the folder in your device that contains images from which you want to extract text.

Now, run the tesseract out command. It will work to draw text from images in the next steps. Furthermore, this command will save the extracted text in the Out.Txt file.

After this is the time to install some modules or packages that are significant to integrate the Tesseract OCR software with Python. We’ll do that in the upcoming step.

3rd Step: Installation of Necessary Modules

Before reaching the final step of extracting text from an image using Python, you need to install two modules that are Pillow and Pytesseract from the CLI Window.

  • For installing pillow modules, run the command pip install pillow.
  • To install the Pytesseract package, run the command pip install pytesseract.

The below table shows the practical running of the commands:

C:\Users\sy\Desktop\test>pip install pillow
Collecting pillow
Downloading Pillow-9.2.0-cp318-cp310-win_amd64.wh1 (3.3 MB)
Installing collected packages: pillow
Successfully installed pillow-9.2.0

C:\Users\sy\Desktop\test>pip install pytesseract
Collecting pytesseract
Downloading pytesseract-0.3.10-py3-none-any.wh1 (14 kB)
Requirement already satisfied: Pillow -8.8.8 in c:\users\sy\appdata\local\programs\python\python318\lib\site-packages (from pytesseract) (9.2.8)
Collecting packaging>=21.3
Downloading packaging-21.3-py3-none-any.wh1 (40 kB)
Collecting pyparsing!-3.0.5,>=2.0.2
Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
Installing collected packages: pyparsing, packaging, pytesseract
Successfully installed packaging-21.3 pyparsing-3.8.9 py-tesseract-8.3.18

Final Step: Data Extraction

Now is the last step which involves typing code for converting an image into text. To do so, follow the below steps:

  • Navigate to the folder that contains the images whose text you want to extract.
  • Create a new file in the folder and rename it to “extract.py”.
  • Now, use the code below and paste it into the newly created file.
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
print(pytesseract.image_to_string(Image.open('test.jpg')))

For running the above-written command, you need to have a photo with the name of “Test.jpg”. Additionally, add this image file to the folder which contains the “extract.py” file.

Practical Example:

We want to extract text from an image. To convert this image into text, we reached the “CLI Window” on our device. There we opened the folder that contains this image and ran the command:

python extract.py

The below table shows this command in action:

C:\Users\sy\Desktop\test>python extract.py
Replicate emphasizes running AI models through an API, offering lines like
Run AI, all with one line of code, and Put any AI model into production instantly.

Alternative Approach to Text Extraction

An alternative and effective way to extract text from images is by using an online image to text converter. Such conversion tools use OCR (Optical Character Recognition) technology to detect characters in an image and convert them into editable text.

A Real-Time Example:

We extracted the text from the same image using an online image to text converter. The tool took no time and did immediate extraction.

Tool Pictorial Demo:

Conclusion

Python is a productive computer programming language that you can use for multiple purposes like app and website development. Nowadays, it is also used for extracting text from images in different methods. In this comprehensive post, we simply explained the complete process of extracting text or data from images. At the end, we discussed an alternative and quick method of image to text extraction, especially for those who don’t have familiarity with the Python language.