使用Tesseract OCR与pytesseract时遇[WinError 5]权限拒绝错误求助
解决pytesseract PermissionError: [WinError 5] 访问被拒绝问题
问题核心
你的代码里犯了一个关键错误:pytesseract.pytesseract.tesseract_cmd被设置成了文件夹路径,但这个参数必须指向tesseract.exe可执行文件的完整路径。系统尝试执行一个文件夹,自然会返回访问被拒绝的错误。
解决方案
定位tesseract.exe文件
找到你安装/解压的Tesseract文件夹,里面肯定有一个tesseract.exe文件。比如:- 官方安装版默认路径:
C:\Program Files\Tesseract-OCR\tesseract.exe - 你放在桌面的解压版:
C:\Users\ccher\OneDrive\Desktop\Tesseract-OCR\tesseract.exe(假设你的Tesseract文件夹叫Tesseract-OCR)
- 官方安装版默认路径:
修正代码中的路径
修改tesseract_cmd的赋值,指向exe文件:import pytesseract, PIL.Image as Image, cv2, matplotlib.pyplot as plt, json, colorama, pyautogui as pg, os colorama.init(autoreset=True) # 修正为tesseract.exe的完整路径 pytesseract.pytesseract.tesseract_cmd = r"C:\\Users\\ccher\\OneDrive\\Desktop\\Tesseract-OCR\\tesseract.exe" print(colorama.Fore.LIGHTGREEN_EX + 'Program Started') screenshot = Image.open('screenshot.png') found_text: str = pytesseract.image_to_string(screenshot)额外建议
- 尽量把Tesseract移到非同步目录(比如
C:\Program Files),OneDrive的同步机制可能导致权限或文件锁定问题。 - 检查
tesseract.exe所在文件夹的权限:右键文件夹→属性→安全→确保当前用户拥有「读取和执行」权限,没有的话添加该权限。
- 尽量把Tesseract移到非同步目录(比如
你的原始代码
import pytesseract, PIL.Image as Image, cv2, matplotlib.pyplot as plt, json, colorama, pyautogui as pg, os colorama.init(autoreset=True) # Setting up the tesseract OCR path pytesseract.pytesseract.tesseract_cmd = r"C:\\Users\\ccher\\OneDrive\\Desktop" print(colorama.Fore.LIGHTGREEN_EX + 'Program Started') screenshot = Image.open('screenshot.png') found_text: str = pytesseract.image_to_string(screenshot)
完整错误信息
Traceback (most recent call last): File "C:\Users\ccher\OneDrive\Desktop\Bloxflip Data Collection\main.py", line 22, in <module> found_text: str = pytesseract.image_to_string(screenshot) File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string return { File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 426, in <lambda> Output.STRING: lambda: run_and_get_output(*args), File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output run_tesseract(**kwargs) File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract proc = subprocess.Popen(cmd_args, **subprocess_args()) File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\ccher\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, PermissionError: [WinError 5] Access is denied
内容的提问来源于stack exchange,提问作者Shampooser Conditioner
相关产品推荐
相关产品推荐

