我正在尝试使用 pytesseract 从图像中读取数据

im trying to read the data from the image using pytesseract

我尝试使用 pytesseract 打开读取图像,但是代码无法读取它,请检查我用于读取文本的这张照片。

下面是我的代码:-

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)

我得到的唯一输出是空的space请帮忙

最终,我找到了问题的答案。

但是这段代码在 Spyder IDE 中不会 运行 正确,所以我们应该确保我们有最新的 tesseract 版本。

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)