Search code examples
python-3.xpytesser

python FileNotFoundError when using pytesseract


I trying to capture a part of the current screen to detect some number on the screen, but when the code run got this error:

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/bot/detect_num.py", line 12, in <module>
    print(pytesseract.image_to_string(Image.open('test.jpg')))
  File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

source code:

import pyscreenshot as ImageGrab
from PIL import Image
import subprocess
from pytesseract import *

if __name__=="__main__":
    im = ImageGrab.grab(bbox=(1349, 34, 1357, 45))
    im = im.convert('1')
    im.save('test.jpg', 'JPEG')
    im.show()
    print(pytesseract.image_to_string(Image.open('test.jpg')))

Please some one tell me why, and how to fix it?


Solution

  • The issue here is that you haven't installed a necessary dependency. When you read pyteseeract's documentation you see the following text:

    • Install google tesseract-ocr from http://code.google.com/p/tesseract-ocr/ . You must be able to invoke the tesseract command as "tesseract". If this isn't the case, for example because tesseract isn't in your PATH, you will have to change the "tesseract_cmd" variable at the top of 'tesseract.py'.

    I presume you haven't yet performed that step, so there's no tesseract command to actually do the OCR work required.