使用pytesseract识别图片文本时遇WinError740权限问题求解决
解决pytesseract调用时的WinError 740权限问题
问题背景
我用Python的pytesseract库识别PNG图片文本时,运行代码抛出OSError: [WinError 740] The requested operation requires elevation错误。
错误堆栈信息
--------------------------------------------------------------------------- OSError Traceback (most recent call last) Cell In[2], line 18 14 pytesseract.tesseract_cmd = path_to_tesseract 16 # Passing the image object to image_to_string() function 17 # This function will extract the text from the image ---> 18 text = pytesseract.image_to_string(img) 20 # Displaying the extracted text 21 print(text[:-1]) File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:423, in image_to_string(image, lang, config, nice, output_type, timeout) 418 """ 419 Returns the result of a Tesseract OCR run on the provided image to string 420 """ 421 args = [image, 'txt', lang, config, nice, timeout] ---> 423 return { 424 Output.BYTES: lambda: run_and_get_output(*(args + [True])), 425 Output.DICT: lambda: {'text': run_and_get_output(*args)}, 426 Output.STRING: lambda: run_and_get_output(*args), 427 }[output_type]() File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:426, in image_to_string.<locals>.<lambda>() 418 """ 419 Returns the result of a Tesseract OCR run on the provided image to string 420 """ 421 args = [image, 'txt', lang, config, nice, timeout] 423 return { 424 Output.BYTES: lambda: run_and_get_output(*(args + [True])), 425 Output.DICT: lambda: {'text': run_and_get_output(*args)}, ---> 426 Output.STRING: lambda: run_and_get_output(*args), 427 }[output_type]() File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:288, in run_and_get_output(image, extension, lang, config, nice, timeout, return_bytes) 277 with save(image) as (temp_name, input_filename): 278 kwargs = { 279 'input_filename': input_filename, 280 'output_filename_base': temp_name, (...) 285 'timeout': timeout, 286 } ---> 288 run_tesseract(**kwargs) 289 filename = f"{kwargs['output_filename_base']}{extsep}{extension}" 290 with open(filename, 'rb') as output_file: File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:255, in run_tesseract(input_filename, output_filename_base, extension, lang, config, nice, timeout) 252 cmd_args.append(extension) 254 try: ---> 255 proc = subprocess.Popen(cmd_args, **subprocess_args()) 256 except OSError as e: 257 if e.errno != ENOENT: File ~\anaconda3\lib\subprocess.py:951, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask) 947 if self.text_mode: 948 self.stderr = io.TextIOWrapper(self.stderr, 949 encoding=encoding, errors=errors) ---> 951 self._execute_child(args, executable, preexec_fn, close_fds, 952 pass_fds, cwd, env, 953 startupinfo, creationflags, shell, 954 p2cread, p2cwrite, 955 c2pread, c2pwrite, 956 errread, errwrite, 957 restore_signals, 958 gid, gids, uid, umask, 959 start_new_session) 960 except: 961 # Cleanup if the child failed starting. 962 for f in filter(None, (self.stdin, self.stdout, self.stderr)): File ~\anaconda3\lib\subprocess.py:1420, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session) 1418 # Start the process 1419 try: -> 1420 hp, ht, pid, tid = _winapi.CreateProcess(executable, args, 1421 # no special security 1422 None, None, 1423 int(not close_fds), 1424 creationflags, 1425 env, 1426 cwd, 1427 startupinfo) 1428 finally: 1429 # Child is launched. Close the parent's copy of those pipe 1430 # handles that only the child should have open. You need (...) 1433 # pipe will not close when the child process exits and the 1434 # ReadFile will hang. 1435 self._close_pipe_fds(p2cread, p2cwrite, 1436 c2pread, c2pwrite, 1437 errread, errwrite) OSError: [WinError 740] The requested operation requires elevation
运行代码
from PIL import Image from pytesseract import pytesseract # Defining paths to tesseract.exe # and the image we would be using path_to_tesseract = r"tesseract.exe" image_path = r"pdf2png/ActaConstitutiva0/ActaConstitutiva0-01.png" # Opening the image & storing it in an image object img = Image.open(image_path) # Providing the tesseract executable # location to pytesseract library pytesseract.tesseract_cmd = path_to_tesseract # Passing the image object to image_to_string() function # This function will extract the text from the image text = pytesseract.image_to_string(img) # Displaying the extracted text print(text[:-1])
环境信息
- Tesseract安装来源:UB-Mannheim的Tesseract发行版
- Python版本:3.10.6
- pytesseract版本:0.3.10
- 运行环境:Jupyter Notebook 6.4.1
- 已尝试常见Stack Overflow解决方案,但无效
解决方案
- 以管理员身份启动Jupyter Notebook:右键点击Jupyter Notebook的快捷方式或启动脚本,选择「以管理员身份运行」后再执行代码,直接提升权限层级。
- 修改Tesseract.exe权限:找到Tesseract安装目录下的
tesseract.exe,右键选「属性」→「兼容性」,勾选「以管理员身份运行此程序」,应用后重启Jupyter再测试。 - 指定完整Tesseract路径:代码中替换
tesseract.exe为绝对路径,比如r"C:\Program Files\Tesseract-OCR\tesseract.exe",避免系统找不到可执行文件或触发权限校验。 - 调整图片存放路径:如果图片在系统保护目录(如
C:\Program Files),将图片移到用户目录(如桌面),修改代码中的image_path后重新运行。
内容的提问来源于stack exchange,提问作者user22290650
相关产品推荐
相关产品推荐

