Categories
Python Answers

How to use pytesseract OCR to recognize text from an image with Python?

Spread the love

Sometimes, we want to use pytesseract OCR to recognize text from an image with Python.

In this article, we’ll look at how to use pytesseract OCR to recognize text from an image with Python.

How to use pytesseract OCR to recognize text from an image with Python?

To use pytesseract OCR to recognize text from an image with Python, we call the image_to_string function.

For instance, we write

import pytesseract
from PIL import Image

text = pytesseract.image_to_string(Image.open("temp.jpg"), lang='eng',
                        config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')

print(text)

to open the image with PIL’s Image.open method.

Then we call image_tp_string with the --psm flag to read the image in as a string.

The string would have the characters in the opened image.

Conclusion

To use pytesseract OCR to recognize text from an image with Python, we call the image_to_string function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *