简体   繁体   中英

Python Error: PermissionError: [WinError 5] Access is denied

So I am currently trying to use Tesseract (pytesseract wrapper) in Python 3.5. Now Im at the office so my guess is that there are some goofy permissions not set and thats why I get this error trying to run some pretty simple code. Now I do have admnin permissions on this machine and can change file permissions... any idea what I can do to get this to run?

If anything it will help me wrap my head around system permissions in general as I work with different OS.

   import pytesseract
from PIL import Image
test = Image.open('test.png')
print (pytesseract.image_to_string(test))


    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
========= RESTART: C:\Users\dmartin\CheckScanScript\TextFromImage.py =========
Traceback (most recent call last):
  File "C:\Users\dmartin\CheckScanScript\TextFromImage.py", line 4, in <module>
    print (pytesseract.image_to_string(test))
  File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Access is denied

I've had same problem. I solved this. First you must add path C:\\Program Files (x86)\\Tesseract-OCR\\ in environment variables. Second I noticed if my code in differen disk, programm can't load language from folder tessdata. So I move my code from Disk D to Disk C, and it's finally work.

我遇到了同样的问题,我通过以管理员身份运行 IDLE 然后通过 IDLE 打开 .py 文件来解决它。

Run Python or Python IDE as Admin and Set tesseract_cmd, pytesseract.pytesseract.tesseract_cmd, TESSDATA_PREFIX and tessdata_dir_config as follows:

from PIL import Image
import pytesseract
tesseract_cmd = 'D:\\Softwares\\Tesseract-OCR\\tesseract'
pytesseract.pytesseract.tesseract_cmd = 'D:\\Softwares\\Tesseract-OCR\\tesseract'
TESSDATA_PREFIX= 'D:\Softwares\Tesseract-OCR'
tessdata_dir_config = '--tessdata-dir "D:\\Softwares\\Tesseract-OCR\\tessdata"'
print(pytesseract.image_to_string( Image.open('D:\\ImageProcessing\\f2.jpg'), lang='eng', config=tessdata_dir_config))

I faced this same issue and adding complete path for the pytesseract executable has worked for me. So , if you have installed pytesseract in your "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract" make sure in your code you are adding below path:-

C:\\Program Files (x86)\\Tesseract-OCR\\tesseract\\tesseract.exe

Your code would look like below

tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract\\tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
print(pytesseract.image_to_string(Image.open('test.png')))

Hope this works for you too.

I solved it by give Permission to the file by code:

import stat
import os

os.chmod("file",stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH|stat.S_IXUSR|stat.S_IRUSR|stat.S_IWUSR|stat.S_IWGRP|stat.S_IXGRP)
os.remove("file")

For me what fixed it was inserting the direct path to tesseract.exe

What it looks like for me is:

import pyautogui
from PIL import Image
from pytesseract import *
pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

Note that you will have to find the path yourself!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM