当我尝试将图像转换为文本 PyTesseract Python 时出错

时间:2021-06-05 15:21:08

标签: python

我正在编写这段非常短的代码:

import pytesseract
from PIL import Image

#converting image to text
print(pytesseract.image_to_string(Image.open('test1.png')))

我有一个大错误:

Traceback (most recent call last):
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Не удается найти указанный файл

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Projects\Captcha Decrypt\decrypt.py", line 8, in <module>
    print(pytesseract.image_to_string(Image.open('test1.png')))
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 409, in image_to_string
    return {
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 412, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 287, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\Rostik\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 259, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: C:\Program Files (x86)\Tesseract-OCR\tesseract is not installed or it's not in your PATH. See README file for more information.  

我已将 Tesseract 添加到 PATH 并且在 cmd 中它工作正常。你能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为这会有所帮助

import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string(r'test1.png'))