Search code examples
pythonpython-3.xpython-tesseract

im trying to read the data from the image using pytesseract


i tried to open read the image using the pytesseract , however the code is not able to read it please check this photo im using for reading the text. enter image description here

below is my code:-

import cv2
import time
import pyscreenshot as ImageGrab
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'C:/Users/RTam/AppData/Local/Programs/Tesseract-OCR/tesseract.exe'

def takescreenshot():
    
    path= (r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos')
    im=ImageGrab.grab(bbox=(900,1000,1200,1100))
    
    im.save(path+'\\'+'ss.png')

img= cv2.imread(r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos\ss3.png')

cv2.imshow('sample',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

sample_text= pytesseract.image_to_string(img)
print(sample_text)

the only output im getting is and empty space please help


Solution

  • Eventually, I found the answer to my question.

    However this code will not run properly in Spyder IDE, so we should make sure we have the latest tesseract version.

    import cv2
    import time
    import pyscreenshot as ImageGrab
    import pytesseract
    pytesseract.pytesseract.tesseract_cmd=r'C:/Users/RTam/AppData/Local/Programs/Tesseract-OCR/tesseract.exe'
    
    def takescreenshot():
        
        path= (r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos')
        im=ImageGrab.grab(bbox=(900,1000,1200,1100))
        
        im.save(path+'\\'+'ss.png')
    
    img= cv2.imread(r'C:\Users\RTam\Desktop\python basics\web scraping\Pyautogui\photos\ss3.png')
    
    def clerify_pic():
        img2 = cv2.resize(img, (0, 0), fx=2, fy=2)
        gry = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
        thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        
        
        return pytesseract.image_to_string(thr)