Python

Extract text from image

Extracting text from an image can be done with image processing.

Sometimes this is called Optical Character Recognition (OCR).
A popular OCR engine is named tesseract.

Related course: Data Science and Machine Learning with Python – Hands On!

OCR with tesseract

Install tesseract on your system. Then you can run the code below.
It starts the tesseract process with the image as argument. The output of the program is returned by the function.

import os
import tempfile
import subprocess

def ocr(path):
temp = tempfile.NamedTemporaryFile(delete=False)

process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()

with open(temp.name + '.txt', 'r') as handle:
contents = handle.read()

os.remove(temp.name + '.txt')
os.remove(temp.name)

return contents

str = ocr('image.png')
print(str)

I’ve used this image to test the program.

Run the program to see the text. All is shown in the terminal.

The famous “Lorem ipsum” text is in the image.

extract text from image

Besides calling the OCR engine directly, you could use one of these modules:

They all use the same OCR engine beneath: tesseract.

Previous Post Next Post

Cookie policy | Privacy policy | ©

Machine Learning