0

pytesseract用来在 python 中将图像读取为文本。以下是我的代码:

from PIL import Image
from pytesseract import image_to_string
import os.path

if (os.path.exists('image.png')):
    filename = 'image.png'
    image = Image.open(filename)
    image.show()
    s = image_to_string(Image.open(filename))
else:
    print('Does not exist')

代码获取文件image.png,打开它并向我显示图像,这意味着该文件存在于该目录中。但是当它转到下一行时s = image_to_string(Image.open(filename)),会出现以下错误。

Traceback (most recent call last):
  File "C:/Users/hp/Desktop/GII/Genetic_Algorithm.py", line 8, in <module>
    s = image_to_string(Image.open(filename))
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我很努力,但不知道如何处理。

4

1 回答 1

0

也许尝试:

f2 = os.path.abspath(文件名)

s = image_to_string(Image.open(f2))

PIL 显然正在使用一些子进程,它可能与主进程没有相同的“默认目录”

于 2016-07-24T18:48:37.973 回答