Perform OCR Using Python

Perform OCR Using Python

Learn to use pytesseract to perform OCR operations on digital data.

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 pytesseract package.

  • We set the PATH of 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 the pytesseract module and pass the image location and the language. We store the result in text.

  • 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.

Did you find this article valuable?

Support Harsh Jain by becoming a sponsor. Any amount is appreciated!