Reputation: 183
Hello I am trying to make a python script that takes an image file from a computer and turn it into text. At the moment, I have the following code
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))
However this gives me the error
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'
What am I doing wrong?
Upvotes: 0
Views: 107
Reputation: 1875
this: pytesseract.pytesseract.tesseract_cmd = filename
is absolutely wrong.
tesseract_cmd should have your tesseract engine executable path.
you need to find where you installed 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>"
you get permission error
because you set the tesseract_cmd
to link the path to your image
and when you actually use pytesseract.image_to_string(<your_image>)
ofcourse it raises permission denied because the image is used by the tesseract_cmd
process.
after you set your engine.
this is gonna be fine
print(pytesseract.image_to_string(filename))
Upvotes: 1