DEV Community

Holden Gerner
Holden Gerner

Posted on • Updated on

Optical Character Recognition (OCR)

What is OCR?

  • Optical Character Recognition is a technology that allows text to be scanned from things like a document or image. An example of this is when you take a picture on your smartphone and you can pull the text straight out. OCR in Python is a helpful tool is pulling text out of an upload document such as a pdf. A commonly used library in Python is Pytesseract.

What is Pytesseract?

Pytesseract is an open-source library used for OCR. It is a wrapper for the Tesseract-OCR engine. Lets look at an example of applying Pytesseract.

First, we need to import the necessary libraries

from PIL import Image
import pyTesseract
import numpy as np
Enter fullscreen mode Exit fullscreen mode

This will be the example image I will be using:
Image description

Next, we will pull the text out of the image:

file = './images-1.png'

img = Image.open(file)
txt = pytesseract.image_to_string(img)

print(txt)
Enter fullscreen mode Exit fullscreen mode

The results is as follows:

bash-3.2$ python3 example.py 

Arial
Enter fullscreen mode Exit fullscreen mode

While this was just a simple example, pytesseract and other libraries used for OCR can make life so much simpler for programmers needing to pull text out of images.

Top comments (0)