AlphaWolf
AlphaWolf

Reputation: 405

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?

Upvotes: 0

Views: 3856

Answers (3)

Tom.chen.kang
Tom.chen.kang

Reputation: 183

https://digi.bib.uni-mannheim.de/tesseract/

from this link, you can get an .exefile, which will make you install the tesseract easier. then you can add the path of the tesseract into environment path. last you can use pytesseract

Upvotes: 0

Tharani Gnanasegaram
Tharani Gnanasegaram

Reputation: 307

When you are using pytesseract, first you have to make sure that you have installed Tesseract-OCR in your system. Then you have to insert the path of the tesseract in your code, as below

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract 
OCR/tesseract'

You can download the Tesseract-OCR form https://github.com/UB-Mannheim/tesseract/wiki

Upvotes: 2

holdenweb
holdenweb

Reputation: 37043

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.

Upvotes: 0

Related Questions