您说: import pytesseract from PIL import Image tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' def demo(): # 打开要识别的图片 image = Image.open(r'C:\Program Files\背书软件\tesseract学习图片.png') # 使用pytesseract调用image_to_string方法进行识别,传入要识别的图片,lang='chi_sim'是设置为中文识别, text = pytesseract.image_to_string(image, lang='chi_sim') # 输入所识别的文字 print(text) if __name__ == '__main__': demo() 我这行代码为什么报错如下: "C:\Program Files\背书软件\.venv\Scripts\python.exe" "C:\Program Files\背书软件\tesseract-orc.py" Traceback (most recent call last): File "C:\Program Files\背书软件\.venv\Lib\site-packages\pytesseract\pytesseract.py", line 275, in run_tesseract proc = subprocess.Popen(cmd_args, **subprocess_args()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python\Lib\subprocess.py", line 1026, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\python\Lib\subprocess.py", line 1538, 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 "C:\Program Files\背书软件\tesseract-orc.py", line 16, in <module> demo() File "C:\Program Files\背书软件\tesseract-orc.py", line 9, in demo text = pytesseract.image_to_string(image, lang='chi_sim') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\背书软件\.venv\Lib\site-packages\pytesseract\pytesseract.py", line 486, in image_to_string return { ^ File "C:\Program Files\背书软件\.venv\Lib\site-packages\pytesseract\pytesseract.py", line 489, in <lambda> Output.STRING: lambda: run_and_get_output(*args), ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\背书软件\.venv\Lib\site-packages\pytesseract\pytesseract.py", line 352, in run_and_get_output run
时间: 2025-03-15 21:19:55 浏览: 18
解决方案
FileNotFoundError
错误通常发生在 pytesseract
无法找到 Tesseract OCR 可执行文件路径的情况下。以下是详细的解决方案:
设置环境变量
确保已经正确安装了 Tesseract 并将其添加到系统的 PATH 环境变量中[^2]。如果未设置,则需要手动配置。
确认 Tesseract 已正确安装 打开命令提示符,输入以下命令来验证 Tesseract 是否正常工作:
tesseract -v
如果返回 Tesseract 版本号,则说明安装成功;否则重新按照指引完成安装。
修改 Path 环境变量 将 Tesseract 的安装目录(默认为
C:\Program Files\Tesseract-OCR
或自定义位置)追加至系统变量Path
中。完成后重启终端或 IDE 来使更改生效。
配置 pytesseract 路径
即使设置了全局 PATH,有时仍需显式指定 Tesseract 的路径给 pytesseract
使用。可以通过调整其内部参数实现这一点。
import pytesseract
from PIL import Image
# 显式设定 Tesseract 的绝对路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image_path = 'example_image.png'
img = Image.open(image_path)
try:
text = pytesseract.image_to_string(img)
print(text)
except Exception as e:
print(f'Error occurred: {e}')
上述脚本通过硬编码方式指定了 Tesseract 的具体地址,从而绕过了依赖于操作系统 PATH 查找机制可能引发的问题[^3]。
注意事项
- 若仍然遇到错误,请检查 Python 和 Tesseract 的位数匹配情况(均为 32-bit 或者 64-bit)。不一致可能导致兼容性问题。
- 对某些特殊字符识别效果不佳的情况可以尝试更新最新版 Tesseract (>= v5.x)[^1]。
相关推荐






