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

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.

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")

I had the same issue and I resolved it by running IDLE as an Administrator and then opening the .py file thru IDLE.

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))

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!

I had the same error. For me, it turned out the file mentioned in the (access denied) error was still held by the system in the background from previous run. I ended the process in the Task Manager of Windows and problem solved.

Related

Python permission error when importing a png

VsCode gives me this error:
PS D:\leomu\Documents\Python projects\Bots> &
C:/Python/Python39/python.exe "d:/leomu/Documents/Python
projects/Bots/verify.py"
File "d:\leomu\Documents\Python projects\Bots\verify.py", line 4, in <module>
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat") File
"C:\Python\Python39\lib\site-packages\PIL\Image.py", line 2904, in
open
fp = builtins.open(filename, "rb") PermissionError: [Errno 13] Permission denied: 'D:\\leomu\\Documents\\Python
projects\\Bots\\FototsChat'
when running this:
import pytesseract, time
from PIL import Image
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat")
text = pytesseract.image_to_string("img")
print(text)
The problem is that you are trying to load a file which is protected and cant be openened/changed by non admin users. To fix that, it depends what OS you are on
For windows users:
Open the terminal/your IDE as administrator and that should do the trick.
For linux/mac users:
Open run put sudo before your command to run the program.
Edit: i read the question better and realised the path to the image points to either a file without extension (like .png) or a folder (i suspect its a folder).
So if you are trying to open the file 'test.png' from within that folder, just do something like 'D:\leomu\Documents\Python projects\Bots\FototsChat\test.png'

WinError 5:Access denied PyTesseract

I know this question has already been answered on this site, however, none of the solutions I looke up the internet seemed to work. Here's what I tried:
Giving all permissions to my python file
Changing PATH variable to point to my tesseract folder
Running IDLE as administrator and then executing the file from there
This error is quite bothering me now and I can't advance any further because of it.
Here's my code if that's going to help:
import pytesseract
import sys
import argparse
try:
import Image
except ImportError:
from PIL import Image
from subprocess import check_output
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR'
c=pytesseract.image_to_string(Image.open('img.png'))
print(c)
Traceback:
Traceback (most recent call last):
File "C:\Users\Hp\Desktop\bot.py", line 12, in <module>
c=pytesseract.image_to_string(Image.open('captcha.png'))
File "C:\Python\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Python\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Python\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Python\lib\subprocess.py", line 992, in _execute_child
startupinfo)
PermissionError: [WinError 5] Accès refusé
I encountered same problem, and I fixed it as 0xc0de said, change the below line:
pytesseract.pytesseract.tesseract_cmd=r"C:\MyApps\Tesseract-ocr\"
to:
pytesseract.pytesseract.tesseract_cmd="C:\\MyApps\\Tesseract-ocr\\tesseract.exe"
I suspect a few things, not sure about any though.
First and the most obvious, the path to Tesseract is not complete. It should be something like:
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
I believe your path points to a directory/folder and not an executable, though only you can confirm that. Let me know if this is incorrect, I see something else too that doesn't seem right at first, but needs more investigation.
Use this to read the tesseract path and also make sure that you have installed the Tesseract- OCR
pytesseract.pytesseract.tesseract_cmd = r'C:\\\Program Files (x86)\\\Tesseract-OCR\\\tesseract.exe'
using always double \\ instead of a single "\"
You need to install tesseract separately. I am providing the link from where you should install
https://github.com/UB-Mannheim/tesseract/wiki
tesseract-ocr-w32-setup-v5.0.0-alpha.20201127.exe (32 bit) and
tesseract-ocr-w64-setup-v5.0.0-alpha.20201127.exe (64 bit) resp.
choose here according to your system config. most of us have 64 bit. so choose that.
Install the file very carefully. I Would suggest doing it in a separate drive other than c.
take the path where you have install the tesseract.exe file. and paste it at pytesseract.pytesseract.tesseract_cmd
look the code...
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'E:/OCR/tesseract_install/tesseract.exe'
img = cv2.imread('E:/OCR/example1.png')
# to see the image below codes are there
cv2.imshow('sampleimage',img)
#enter any key to destroy the image window opened due to previous line code
cv2.waitKey(0)
cv2.destroyAllWindows()
#convert image to text using tesseract
text = pytesseract.image_to_string(img)
print(text)
I had this issues pretty consistent, and I fix it without any access change.
The trick is:
1: You need to install tesseract and remember where is path to tesseract.exe file.
2: Then install pytesseract on exect environment you going to use it using pip install pytesseract
3: Do not add any Path to your System Env variables, it will mess up with everything.
4: CLEARLY define path to image file, or even better if image will be in closest/the same directory where is your python code.
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'C:/Users/your_name/AppData/Local/Programs/Tesseract-OCR/tesseract'
#this path to image
path = r'C:\Users\irina_max\Documents\Python_Scripts\Invoice 1.jpg'
image= Image.open(path)
image
Install the tesseract exe in a directory inside your project. I did the above and it worked fine, without giving Win 5 error.

pytesseract error Windows Error [Error 2]

Hi I am trying the python library pytesseract to extract text from image.
Please find the code:
from PIL import Image
from pytesseract import image_to_string
print image_to_string(Image.open(r'D:\new_folder\img.png'))
But the following error came:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I did not found a specific solution to this. Can anyone help me what to do. Anything more to be downloaded or from where i can download it etc..
Thanks in advance :)
I had the same trouble and quickly found the solution after reading this post:
OSError: [Errno 2] No such file or directory using pytesser
Just need to adapt it to Windows, replace the following code:
tesseract_cmd = 'tesseract'
with:
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
(need double \\ to escape first \ in the string)
You're getting exception because subprocess isn't able to find the binaries (tesser executable).
The installation is a 3 step process:
1.Download/Install system level libs/binaries:
For various OS here's the help. For MacOS you can directly install it using brew.
Install Google Tesseract OCR (additional info how to install the
engine on Linux, Mac OSX and Windows). You must be able to invoke the
tesseract command as tesseract. If this isn’t the case, for example
because tesseract isn’t in your PATH, you will have to change the
“tesseract_cmd” variable at the top of tesseract.py. Under
Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users.
please install homebrew package tesseract.
For Windows:
An installer for the old version 3.02 is available for Windows from
our download page. This includes the English training data. If you
want to use another language, download the appropriate training data,
unpack it using 7-zip, and copy the .traineddata file into the
'tessdata' directory, probably C:\Program Files\Tesseract-OCR\tessdata.
To access tesseract-OCR from any location you may have to add the
directory where the tesseract-OCR binaries are located to the Path
variables, probably C:\Program Files\Tesseract-OCR.
Can download the .exe from here.
2.Install Python package
pip install pytesseract
3.Finally, you need to have tesseract binary in you PATH.
Or, you can set it at run-time:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '<path-to-tesseract-bin>'
For Windows:
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
The above line will make it work temporarily, for permanent solution add the tesseract.exe to the PATH - such as PATH=%PATH%;"C:\Program Files (x86)\Tesseract-OCR".
Beside that make sure that TESSDATA_PREFIX Windows environment variable is set to the directory, containing tessdata directory. For example:
TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR
i.e. tessdata location is: C:\Program Files (x86)\Tesseract-OCR\tessdata
Your example:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
print pytesseract.image_to_string(Image.open(r'D:\new_folder\img.png'))
You need Tesseract OCR engine ("Tesseract.exe") installed in your machine. If the path is not configured in your machine, provide complete path in pytesseract.py(tesseract.py).
README
Install Google Tesseract OCR (additional info how to install the engine on Linux, Mac OSX and Windows). You must be able to invoke the tesseract command as tesseract. If this isn't the case, for example because tesseract isn't in your PATH, you will have to change the "tesseract_cmd" variable at the top of tesseract.py. Under Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users. please install homebrew package tesseract.
Another thread
I have also faced the same problem regarding pytesseract.
I would suggest you to work in linux environment, to solve such errors.
Do the following commands in linux:
pip install pytesseract
sudo apt-get update
sudo apt-get install pytesseract-ocr
Hope this will do the work..

Can't import matplotlib.pyplot, IOError related to a font

I recently installed the Anaconda distribution of Python. When I try to import matplotlib.pyplot, I receive a "Permission denied" error as the font manager tries to access one of the fonts on my computer.
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 19, in <module>
import matplotlib.font_manager as font_manager
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1412, in <module>
_rebuild()
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1397, in _rebuild
fontManager = FontManager()
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1052, in __init__
self.ttflist = createFontList(self.ttffiles)
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 579, in createFontList
font = ft2font.FT2Font(fpath)
IOError: [Errno 13] Permission denied: u'/Library/Fonts/Finale Lyrics Italic.ttf'
How can I get matplotlib.pyplot to load and not stop at this "Permission denied" font error? I don't need any particular font (eg, I don't need to use "Finale Lyrics Italics" - any font is fine). Any thoughts would be much appreciated!
I have the same problem in Linux Mint 17.2. I do this in my terminal :
sudo chmod 644 /my-fonts-path/*
For you, try :
sudo chmod 644 /Library/Fonts/Finale/*
More information can be found here http://ubuntuforums.org/showthread.php?t=1976037
The obvious thing to do here is to actually fix the problem instead of working around it. You know the path to the offending file; just chmod it.
But if you need to work around it instead (e.g., you're deploying your program to lots of machines, any of which may have this problem)… well, if you look at the source, the problem is in font_manager.createFontList. For non-AFM fonts, the FT2Font constructor is wrapped in a try that handles RuntimeError and UnicodeError, but not IOError.*
You could argue that this is a bug in matplotlib. I'm not sure about that, but if you think it is, file a bug and post to the mailing list.
But either way, you need a fix, whether you just use it locally, or also submit it upstream. The patch is simple. In that function, just change this:
try:
font = ft2font.FT2Font(fpath)
except RuntimeError:
verbose.report("Could not open font file %s"%fpath)
continue
… to:
try:
font = ft2font.FT2Font(fpath)
except (RuntimeError, IOError):
verbose.report("Could not open font file %s"%fpath)
continue
So, there are two ways to do this.
If you want to patch your copy of matplotlib, fork the repo on Github, make a branch, edit your copy of the file, commit to your fork, make sure you have all the dependencies up to date, and either pip install . from the top level of your fork or install directly from git. (If you've filed a bug, you should also create a pull request, or create a patchfile and upload it to the bug report.)**
If you instead want to monkeypatch it from your own code, copy the entire createFontList function into your code, edit the copy, then add matplotlib.font_manager.createFontList = createFontList after the definition.
* You could instead patch ft2font.FT2Font to raise a RuntimeError in this case, but that's implemented in C, not Python, so it's going to be more of a pain.
** As user3267581 suggests, instead of editing and rebuilding the project, you could just edit the .py file in your site-packages. Of course this will only work on one machine, will make it easy to forget the workaround if you need it later, and may require you to know something about how site packages work, but if all of that sounds OK, it's obviously a lot less work.

Why can't this python script find the libclang dll?

I would like to get started with using libclang with Python. I am trying to get a sample code (http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/) to work on Windows, here is a part of the code I'm trying to run:
#!/usr/bin/python
# vim: set fileencoding=utf-8
import sys
import os
import clang.cindex
import itertools
...
print("Setting clang path")
# I tried multiple variations. Libclang is correctly installed in the specified location.
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin')
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin/libclang.dll')
# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")
index = clang.cindex.Index.create()
...
I stripped all the other parts of the code, but I can post them if they are relevant. The line
index = clang.cindex.Index.create()
Throws the following error:
Setting clang path
Clang path set
Traceback (most recent call last):
File "D:\libclangtest\boost_python_gen.py", line 60, in <module>
index = clang.cindex.Index.create()
File "D:\libclangtest\clang\cindex.py", line 2095, in create
return Index(conf.lib.clang_createIndex(excludeDecls, 0))
File "D:\libclangtest\clang\cindex.py", line 141, in __get__
value = self.wrapped(instance)
File "D:\libclangtest\clang\cindex.py", line 3392, in lib
lib = self.get_cindex_library()
File "D:\libclangtest\clang\cindex.py", line 3423, in get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError: [Error 193] %1 is not a valid Win32 application. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
What is the reason for this? Am I setting the dll's path wrong? I tried multiple ways, with foreslashes and backslashes, I also tried to move the dll out of Program Files to make the path contain no spaces, but nothing worked.
I am a total beginner to libclang and Python, sry if I'm asking something trivial.
I was running into a similar problem (Windows 7 x64, Anaconda3 x64). Using
import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
fixed the problem. Please note that you need to use slashes (not antislashes), and specify path to bin/libclang.dll (not to lib/libclang.dll).
#SK-logic commented that I should check whether both Python and libclang are either 32bit or 64bit. Libclang was 32bit, but I couldn't find a way to check whether my Python installation is 32 or 64, so I reinstalled the 32bit version, and now it works. So the problem probably was that I had the 64 bit version of Python.

Categories

Resources