Extract Text from an Image using a Python Program

Extract text from an image using python pytesseract tool.

Introduction

Are you really want to extract text from an image? If, yes, this article gonna help you. You can extract text from an image using Python language.

In fact, it is really needed when we fall in love with the text of an image instead of the background. This often happens on social media. There are a lot of posts shared where some quotes or information are presented as images rather than raw text. In that case, there are two ways to remember those words. Either download that image or note down quotes or information from there(but this is a time-consuming task).

But, with the help of programming, it is possible to easily extract all text from an image. Here, we will use a Python Image Processing tool called pytesseract to do this challenging task. It is not as difficult as it seems. You must have Python installed on your system and then install that third-party tool. See below for this.

👉Visit AlsoHack with Image: Extract Metadata from an Image using Python

Requirements

Install this beautiful python tool to get text from an image in python. Use pip3 instead of pip for Linux.

Install pytesseract: pip install pytesseract

To show you an example, I created an image with some text for showing you the output.

this is a sThis is a sample image and used to extract text from it in the program.ample image which is used in the program to extract text from it.
Sample Image

The Code

This program will work on every operating system without changing any line of code; you just need to mention the image path properly here. You can also use the Sample Image I used here for your practice. Here is the Original Image(GitHub Page).

Recommendation for You: Create a different directory for this program and inside there create an another folder named 'Images'. Place all the images you want to work with here.


'''A Python Program to extract text from an image'''
from PIL import Image
import pytesseract as p

# The image path
img = 'Images/sample.png'

print("\nExtracting...")
# Opening the image
data = Image.open(img)
# Extracting the text from the image data
result = p.image_to_string(data)

print("\n===========The Extracted data===========\n")
# Printing the result
print(result)

Output


Extracting...

===========The Extracted data===========

Stop

Wasting your time
 

Output through video

Watch the entire video to know how the program actually works.

Summary

In today's lesson, you learned how to extract text from an image using a python program. To do this job, we used pytesseract tool offered by python. Here, I showed you just one example with an image. If you want to explore more, there are more images I uploaded to my GitHub page. You can get all of those from here.

If you have any queries related to this topic, please leave your message below. You will get an immediate response.

To get more lovely python topics, visit the separate page created only for Unique Examples. Some examples are given below.

👉Wish Your Friends with Stylish TExt in Python

👉Draw the Sketch of Lionel Messi using a Python Program

👉Create a Time Wasting Websites Blocker using Python

👉Extract emails and phone numbers from a webpage using python

Have a nice day ahead, cheers!

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post