FileNotFoundError:[WinError 2]使用pytesseract python-3.x时系统找不到指定的文件

时间:2016-07-24 18:33:06

标签: python-3.x image-processing pytesser

我使用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

我努力但却不知道如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

也许试试:

f2 = os.path.abspath(filename)

s = image_to_string(Image.open(f2))

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