Python PermissionError:[Errno 13]对图像使用tkinter时

Python PermissionError:[Errno 13]对图像使用tkinter时,python,image,tkinter,Python,Image,Tkinter,您好,我正在尝试制作一个python脚本,它从计算机获取一个图像文件并将其转换为文本。目前,我有以下代码 from tkinter import Tk from tkinter.filedialog import askopenfilename import pytesseract Tk().withdraw() filename = askopenfilename() print(filename) pytesseract.pytesseract.tesseract_cmd = filen

您好,我正在尝试制作一个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'
回溯(最近一次呼叫最后一次):
文件“main.py”,第11行,在
打印(pytesseract.image_到_字符串(文件名))
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/pytesseract/pytesseract.py”,第409行,在图像_到_字符串中
返回{
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/pytesseract/pytesseract.py”,第412行,在
Output.STRING:lambda:run_和_get_Output(*args),
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/pytesseract/pytesseract.py”,第287行,在运行和获取输出中
运行_tesseract(**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/pytesseract/pytesseract.py”,第258行,在run_tesseract中
提高e
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/pytesseract/pytesseract.py”,第255行,在run_tesseract中
proc=subprocess.Popen(cmd_args,**subprocess_args())
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py”,第854行,在__
self.\u execute\u child(参数、可执行文件、预执行文件、关闭文件、,
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py”,第1702行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
权限错误:[Errno 13]权限被拒绝:'/Users/william/theimage.jpg'

我做错了什么?

这个:
pytesseract.pytesseract.tesseract\u cmd=filename
绝对是错误的

tesseract\u cmd应具有tesseract引擎的可执行路径

您需要找到安装tesseract的位置

pytesseract.pytesseract.tesseract\u cmd=
#范例
#pytesseract.pytesseract.tesseract_cmd=“/some_folder1/some_folder2/。。。
#//tesseract。“
您会得到
权限错误
,因为您设置了
tesseract\u cmd
将路径链接到
您的图像
,并且当您实际使用
pytesseract.image\u to\u string()
时,它当然会导致权限被拒绝,因为
tesseract\u cmd
进程使用了该图像

在你设置好引擎之后

一切都会好起来的

打印(pytesseract.image_到_字符串(文件名))

你确定应该将
tessearct\u cmd
设置为图像文件吗?我认为应该指向实际的
tesseract
命令。这个错误告诉你它无法执行图像,这是有意义的,因为图像不是可执行的。非常感谢,我使用了这个代码text=pytesseract.image\u to\u字符串(Image.open(filename))现在可以工作了