import cv2
import pytesseract
import os
from PIL import Image
preprocess = 'blur'
image = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
if preprocess == "blur":
gray = cv2.medianBlur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
Traceback (most recent call last):
File "D:\pycharm_prj\wenzishibie\test.py", line 20, in <module>
text = pytesseract.image_to_string(Image.open(filename))
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 416, in image_to_string
return {
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 419, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 286, in run_and_get_output
run_tesseract(**kwargs)
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 257, in run_tesseract
raise e
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 254, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "D:\python\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\python\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] 拒绝访问。
用管理员权限运行代码试试