首页 > 解决方案 > PermissionError: [Errno 13] 将 tkinter 用于图像时

问题描述

您好我正在尝试制作一个 python 脚本,该脚本从计算机获取图像文件并将其转换为文本。目前,我有以下代码

from tkinter import Tk
from tkinter.filedialog import askopenfilename
import pytesseract


Tk().withdraw()
filename = askopenfilename()
print(filename)
pytesseract.pytesseract.tesseract_cmd = filename

print(pytesseract.image_to_string(filename))

然而,这给了我错误

Traceback (most recent call last):
  File "main.py", line 11, in <module>
    print(pytesseract.image_to_string(filename))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 409, in image_to_string
    return {
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 412, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 287, in run_and_get_output
    run_tesseract(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 258, in run_tesseract
    raise e
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 255, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/Users/william/theimage.jpg'

我究竟做错了什么?

标签: pythonimagetkinter

解决方案


这:pytesseract.pytesseract.tesseract_cmd = filename是绝对错误的。

tesseract_cmd 应该有您的 tesseract 引擎可执行路径。

您需要找到安装 tesseract 的位置。

pytesseract.pytesseract.tesseract_cmd = <path_to_tesseract_engine>
# example 
# pytesseract.pytesseract.tesseract_cmd = "/some_folder1/some_folder2/...
# /<where_you_installed_tesseract_cmd>/tesseract.<whatever_is_the_extension_for_executables_on_mac>"

你得到permission error是因为你设置了tesseract_cmd链接路径your image,当你实际使用它时,pytesseract.image_to_string(<your_image>)它会引发权限被拒绝,因为图像被tesseract_cmd进程使用。

设置好引擎后。

this is gonna be fine

print(pytesseract.image_to_string(filename))

推荐阅读