python - 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'
我究竟做错了什么?
解决方案
这: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))
推荐阅读
- java - 每5分钟重启一次android后如何检查互联网连接?
- java - 如何设置索引值以更改每个列标题的颜色以及如何从 API 请求中设置颜色?
- swift - NavigationView 上的编程导航比两个视图更深
- r - R 中的 %in% 问题
- nginx - 秘密 argocd/argo-cert-prod 已更新并用于入口注释
- rust - 在两个哈希图之间交换特征
- kubernetes - 如何在入口配置片段中获取请求 URI?
- amazon-web-services - 如何在 serverless.yml 文件中引用函数 (Lambda) 的函数 ARN?
- c++ - 构造函数的部分模板特化
- java - @GeneratedValue(strategy = GenerationType.AUTO):MySQL 和生成的 ID 在表之间共享?