Python pytesseract problem to open '[Errno 13] Permission denied:' in Mac
Question:
I am having trouble with pytesseract in python. When I call the file in the code it says permission is denied. I am working on Mac, so I do not know what to do but I hope you can help me.
#from every single image-based cell/box the strings are extracted via pytesseract and stored in a list
outer=[]
for i in range(len(finalboxes)):
for j in range(len(finalboxes[i])):
inner=''
if(len(finalboxes[i][j])==0):
outer.append(' ')
else:
for k in range(len(finalboxes[i][j])):
y,x,w,h = finalboxes[i][j][k][0],finalboxes[i][j][k][1], finalboxes[i][j][k][2],finalboxes[i][j][k][3]
finalimg = bitnot[x:x+h, y:y+w]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
border = cv2.copyMakeBorder(finalimg,2,2,2,2, cv2.BORDER_CONSTANT,value=[255,255])
resizing = cv2.resize(border, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
dilation = cv2.dilate(resizing, kernel,iterations=1)
erosion = cv2.erode(dilation, kernel,iterations=1)
out = pytesseract.image_to_string(erosion) # This line is the one that gives the error
if(len(out)==0):
out = pytesseract.image_to_string(erosion, config='--psm 3')
inner = inner +" "+ out
outer.append(inner)
Thanks
Answers:
Are you using an assignment to pytesseract of the executable file for tesseract?
Since I don’t see it in your code, I will assume you are not. I will also assume that you used Homebrew to install tesseract OCR. If you used macports or are using on another os, check official install documentation here.
Your entire script should at least contain the following:
- An import of the pytesseract module.
import pytesseract
- An assignment to the pytesseract module instance of the tesseract executable. This must refer to the path and the tesseract executable binary for it to function adequately:
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/[version]/bin/tesseract'
- You may now call upon any method of the pytesseract methods, for example:
print(pytesseract.image_to_string(r'/Path/to/image/you/want/to/process/Picture1.png'))
For Linux users, Please execute Which Tesseract in terminal which will provide you the Tesseract executable path. This resolved the error.
What worked for me was using which and then used that path:
which tesseract
the path it brings back is what you should use.
I am having trouble with pytesseract in python. When I call the file in the code it says permission is denied. I am working on Mac, so I do not know what to do but I hope you can help me.
#from every single image-based cell/box the strings are extracted via pytesseract and stored in a list
outer=[]
for i in range(len(finalboxes)):
for j in range(len(finalboxes[i])):
inner=''
if(len(finalboxes[i][j])==0):
outer.append(' ')
else:
for k in range(len(finalboxes[i][j])):
y,x,w,h = finalboxes[i][j][k][0],finalboxes[i][j][k][1], finalboxes[i][j][k][2],finalboxes[i][j][k][3]
finalimg = bitnot[x:x+h, y:y+w]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
border = cv2.copyMakeBorder(finalimg,2,2,2,2, cv2.BORDER_CONSTANT,value=[255,255])
resizing = cv2.resize(border, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
dilation = cv2.dilate(resizing, kernel,iterations=1)
erosion = cv2.erode(dilation, kernel,iterations=1)
out = pytesseract.image_to_string(erosion) # This line is the one that gives the error
if(len(out)==0):
out = pytesseract.image_to_string(erosion, config='--psm 3')
inner = inner +" "+ out
outer.append(inner)
Thanks
Are you using an assignment to pytesseract of the executable file for tesseract?
Since I don’t see it in your code, I will assume you are not. I will also assume that you used Homebrew to install tesseract OCR. If you used macports or are using on another os, check official install documentation here.
Your entire script should at least contain the following:
- An import of the pytesseract module.
import pytesseract
- An assignment to the pytesseract module instance of the tesseract executable. This must refer to the path and the tesseract executable binary for it to function adequately:
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/[version]/bin/tesseract'
- You may now call upon any method of the pytesseract methods, for example:
print(pytesseract.image_to_string(r'/Path/to/image/you/want/to/process/Picture1.png'))
For Linux users, Please execute Which Tesseract in terminal which will provide you the Tesseract executable path. This resolved the error.
What worked for me was using which and then used that path:
which tesseract
the path it brings back is what you should use.