Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and “read” the text embedded in images.

Python-tesseract is a wrapper for Google’s Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, bmp, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file.

import pytesseract
from pytesseract import Output
import cv2
import numpy as np
import pandas as pd
import io
from io import BytesIO
import sys
import os
import tempfile
import csv
import json
from csv import writer

def main(filename):

    #for windows user
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
    img = cv2.imread(filename)
    d = pytesseract.image_to_data(img, output_type=Output.DICT)  
    json_format=json.dumps(d)    
    print(json_format)
    #with open("sample.json", "w") as outfile: 
    #json.dump(d, outfile)


if __name__ == "__main__":
    #id=2
    image_path = 'image.png'
    main(image_path )