Python Tesseract: Read Text from Image

Reading text from images using the Tesseract

Code

import cv2
import pytesseract

if __name__ == '__main__':
	# setup the path for the tesseract tool
    pytesseract.pytesseract.tesseract_cmd = r"C:\Tesseract-OCR\tesseract.exe"
    #load the image
    img = cv2.imread("code.png")
    #convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # read the text
    text = pytesseract.image_to_string(gray)
    # print the text
    print(text)
    # show the converted image
    cv2.imshow("Img", gray)
    # wait for input to keep shown image up and available
    cv2.waitKey(0)

Original image

Imported grayscaled image

Extracted text

‘import cv2
import pytesseract

 

if __name__ == '__main__'
# setup the path for the tesseract tool
pytesseract.pytesseract.tesseract_cmd = n"C:\Tesseract-OcR\tesseract.exe"
# load the image
ing = cv2.inread("code.png")
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY
# read the text
text = pytesseract.image_to_string(gray)
# print the text
print(text)
# show the converted image
cv2.imshow("Ing", gray)
# wait for input to keep shown image up and available
ov2.waitkey (0)

Definitely doesn't compile, but it is good enough to infer and use I think

Hat tip to cv2 and pytesseract