You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下PermissionError: [Errno 13]权限拒绝问题求助

解决PermissionError: [Errno 13] Permission denied问题(pytesseract相关)

核心问题是你错误地将tesseract_cmd指向了Python包目录中的文件,而非Tesseract OCR引擎的可执行程序路径。pytesseract只是Python封装库,实际需要调用系统中安装的Tesseract二进制执行文件。

解决步骤:

  1. 获取Tesseract正确路径
    在终端执行以下命令,直接得到Tesseract可执行文件的实际路径:

    which tesseract
    

    macOS下通过Homebrew安装的Tesseract,默认路径通常是/usr/local/bin/tesseract;官网安装包的默认路径可能是/usr/bin/tesseract。

  2. 修正代码中的路径配置
    将代码中tesseract_cmd的赋值替换为终端输出的正确路径,示例:

    pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract'
    
  3. 额外排查项(若问题仍存在)

    • 确认Tesseract已正确安装:终端执行tesseract --version,能正常输出版本信息则说明安装有效。
    • 未安装Tesseract的话,通过Homebrew安装(推荐):
      brew install tesseract
      
      或者从官方渠道下载对应macOS的安装包完成安装。

修正后的完整代码示例:

import cv2
from PIL import Image
import pytesseract

# 替换为你的Tesseract实际可执行路径
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract'
img = cv2.imread(r'/Users/lhldanh/CompSci/Python/Coursera/TextDetection/sample.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
 
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
 
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)
 
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 
im2 = img.copy()

file = open("recognized.txt", "w+")
file.write("")
file.close()

for cnt in contours:
    x, y, w, h = cv2.boundingRect(cnt)
    # 修正原代码中矩形绘制的y轴终点错误(y+w改为y+h)
    rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cropped = im2[y:y + h, x:x + w]
    file = open('recognized.txt', 'a')
    text = pytesseract.image_to_string(cropped)
    file.write(text)
    file.write('\n')
    file.close()

内容的提问来源于stack exchange,提问作者Liêu Hải Lưu Danh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:30:43