Reading Texts on Image by Using Tesseract and PyOCR in Python

    Optical Character Recognition (OCR) is a conversion of typed or handwritten letters on an image into the machine encoded texts.  There are several methods and libraries that can be used to read text on image.

    In this tutorial, we'll briefly learn how to read letters in an image by using the Tesseract and PyOCR in Python. The tutorial covers:

  1. Installing Tesseract and PyOCR
  2. Reading texts on image
  3. Source code listing
   Let's get started.

  

  Installing Tesseract and PyOCR


   My installation goes on MacBook and you can find similar installation methods on other OSs.  Here, we install Tesseract and python PyOCR library. 

 
% brew install tesseract

% pip install pyocr
  
 
If you want to use the Tesseract directly to read the texts on your image, you can run it as below. It parses the texts on your image and shows them on your terminal output.
 
 
% tesseract /your/path/image.png stdout 
  


Reading texts on image
 
    After installing Tesseract and PyOCR, we'll load the required libraries for this tutorial.


from PIL import Image
import pyocr
import pyocr.builders 
 

We also need image file that contains texts and I prepared below image. 



Next, we'll get available tools and languages.


tools = pyocr.get_available_tools()
print(len(tools))
 
tool = tools[0]
print("Tools: ", tool.get_name())
print("Available languages: ", tool.get_available_languages()) 

Tools:  Tesseract (sh)
Available languages: ['eng', 'osd', 'snum']
  

We'll load image file with PIL library's Image.  Then we'll parse text by using the image_to_string() function of the tools object. Finally, we'll print the output text.  


path = '/my/image/path/image_to_read.png'
img = Image.open(path)

txt = tool.image_to_string(
    img,
    lang='eng',
    builder=pyocr.builders.TextBuilder()
)
 
print(txt)
 

Presentation Subtitle

CAN YOU READ IT

ONE TWO THREE

Title
  

    In this tutorial, we've briefly learned how to read text on image file by using Tesseract and PyOCR tools. Full source code is listed below.


Source code listing
 
 
from PIL import Image
import pyocr
import pyocr.builders

tools = pyocr.get_available_tools()

print(len(tools))

tool = tools[0]
print("Tools: ", tool.get_name())
print("Available languages: ", tool.get_available_languages())

path = '/my/image/path/image_to_read.png'
img = Image.open(path)

txt = tool.image_to_string(
    img,
    lang='eng',
    builder=pyocr.builders.TextBuilder()
)
 
print(txt)
 

No comments:

Post a Comment