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.
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 to show you the output.
![]() |
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 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 Cool Python Programs. 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 text using python
Have a nice day ahead, cheers!
PySeek