Perform OCR Using Python
Learn to use pytesseract to perform OCR operations on digital data.

Hi, I'm Harsh, a software developer at Springworks, and an Ex-TCSer.
I am a coding instructor and mentor and have been creating multiple online courses to get people comfortable learning how to code and help them get better opportunities.
I have been coding since I was 15 when I created a static website for a school project. I was given positive feedback on this project, which pushed me to major in computer science with a specialization in Artificial Intelligence.
For me, "The day is not over if I have not done any coding. I usually try to solve Competitive Programming problems, which helps me to improve my problem-solving skills. Every day I try to learn something new."
As a Software Developer for Tata Consultancy Services Limited, I have built scalable backend services using Node.js and Microsoft Azure. Apart from this, I am also an author of 6 courses at Educative.io and have been building courses on the latest technologies.
I’m familiar with various programming languages, including JavaScript, Python, and a bunch of other technical areas like System Design, Databases. I’m always adding new skills to my repertoire.
I've been Microsoft Certified in Azure Fundamentals and Azure AI Fundamentals. I am also now a Microsoft Certified Azure AI Engineer Associate.
I have delivered over 20 one-on-one sessions. If you want to talk more about coding, interview preparation, software development, or just want any career guidance, especially from the technical domain, hit me up or just connect with me at: topmate.io/harsh_jain
We have already installed all the required setups to run the code to perform OCR on some sample images. You can save the below image taken in this case.

Let us jump directly into the code.
import pytesseract
# pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
img_path = 'test.png'
lang = 'eng'
text = pytesseract.image_to_string(img_path, lang=lang)
print(text)
First, we imported the
pytesseractpackage.We set the
PATHof the Tesseract executable file. You can find this path while installing the Tesseract OCR Engine.We gave the location of our image file on which we want to perform OCR.
We defined the language of the text present in the image.
We called the function
image_to_string()from thepytesseractmodule and pass the image location and the language. We store the result intext.Finally, we printed the text on the console. Below is the output generated by executing the code:

We can observe that the output is pretty much correct and Tesseract has extracted the text from the image.
We will continue the next steps in the next article in this series.
This series is just a snapshot of the Build a REST API Using Python and Deploy it to Microsoft Azure course which covers a lot more things like FastAPI, Microsoft Azure, Deploying FastAPI applications to Azure, Monitoring the applications using Azure, and more projects. Do check it out and let me know if you have any questions.



