×

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical image into encoded form of text is called as OCR. OCR stands for Optical Character Recognition. OCR works under the research categories of Artificial intelligence and machine learning, pattern recognition and computer technologies. This is done in such a way that the image is processed under the machine learning algorithms, and it is certainly known that some kind of information is present in the image.

For understanding the image to text process in Python we need to import two different types of libraries and as followed:

  • Pytesseract
  • Pillow

Tesseract is an open-Source tool which is used to process and extract text from images. To make use of Tesseract in Python we need the help of pytesseract which is one of a wrappers for the tesseract. As we are working the image processing, we need the help of pillow library which is used to perform operation on it in Python.

The commands for installing the pytesseract and pillow in Python are:

pip install pytesseract
pip install pillow

We need to mainly focus on the paths of the files as it will be helpful in the further execution of code. For most of the directories the path is maintained as:

C:\\ProgramFiles\\Tesseract\\tesseract.exe

Process to be Followed:

  • We need to import the image firstly using PIL library for viewing image and opening the image.
  • Then by using the pytesseract module from the main tesseract library which we use it for the text prediction and extraction from the image.
  • After importing libraries and viewing the image, we need to define the path for the tesseract module which we installed in the first and this path will be depending upon on the location where it is installed and we need to define the path for the image file also.
  • We need to define the path for tesseract cmd variable, this helps in finding the text in image and to extract it.
  • After defining all the paths, we need to pass the image object to image string function. This takes input as image object and returns the text data present it.
  • At last, we display the text which is extracted from the image.

Example:

We are uploading an image for the reference and the code:

Image to Text in Python

Code:

from PIL import Image
from pytesseract import pytesseract
#defining the path for image and tesseract.exe
Path_to_tesseract = r” C:\\ProgramFiles\\Tesseract\\tesseract.exe”
imageP = r”csv\sample_texting.png”
 Image = image. open(imagep) #Viewing the image and storing the image data                                                 
#Accessing the tesseract directory
#pytesseract library setting location
Pytesseract . tesseract_cmd = path_to_tesseract
Text = pytesseract. image_to_string(image) 
#providing the image object to image_to_string ()
#Display output
print (text [; -1])

Output:

Life is beautiful

The above example code is for reading the single image and displaying the desired output.

Extracting the Text from Multiple Images at Once:

We need to import a new library named as os. This module is used for interacting with the operating system and has many built in functions embedded in it to interact with the file system. We import this library when we are needed to extract the data from multiple images.

The code for image is saved by main.py.

Here, we use iteration technique for reading the images and following the same module method as of the single image.

Example:

Image to Text in Python
Image to Text in Python
Image to Text in Python

Code:

#images uploaded
from PIL import Image
from pytesseract import pytesseract
import os
#defining the path for image and tesseract.exe
Path_to_tesseract = r” C:\\ProgramFiles\\Tesseract\\tesseract.exe”
imageP = r”images/”
#Accessing the tesseract directory
#pytesseract library setting location
Pytesseract . tesseract_cmd = path_to_tesseract
For root, dirs, file_name in os. Walk(path_to_image):
#iterate  over each file_name in the folder
For file_name in the file_name:
#Open image with PIL
img = Image.open(path_to_image+file_name)
#Extract text from image
Text = pytesseract. image_to_string(img)
Print(text)

Output:

Sample Text 1
Sample Text 2
Sample Text 3

Tesseract performs well and good when the image document has high-quality resolution and no noise in it and the scaling of the is done in appropriately.

The tesseract version 4.0 which is the latest one is very much accurate and tesseract as a good tool when it comes to scanning of documents.


Related Topics

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Create First GUI Application using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python Project Ideas for Advanced Developers

Best Python Project Ideas for Advanced Developers Python is an object-oriented, high-level, interpreted programming language created by a developer known Guido Van Rossum. Python is one of the languages that gathered...

9 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.