Mac通过Homebrew安装Tesseract后无法使用pytesseract的问题
解决Mac下Homebrew安装Tesseract后pytesseract找不到的问题
问题原因
Homebrew安装的Tesseract路径未被加入系统默认PATH,导致pytesseract无法定位到Tesseract可执行文件。
解决方案
方案一:在Python代码中直接指定Tesseract路径
先通过终端命令确认Tesseract的安装路径:
brew info tesseract
输出结果里会显示类似/usr/local/bin/tesseract(Intel芯片Mac)或/opt/homebrew/bin/tesseract(M系列芯片Mac)的路径。
修改你的Python代码,添加路径指定:
import cv2 import pytesseract # 替换成你实际的Tesseract路径 pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract' img = cv2.imread("你的png文件路径") print(pytesseract.image_to_string(img))
方案二:将Tesseract路径加入系统PATH(永久生效)
根据你的Mac芯片类型,执行对应命令:
- Intel芯片:
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
- M系列芯片:
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
执行完后,在终端运行tesseract --version验证,能正常显示版本信息后,再运行你的Python代码即可。
内容的提问来源于stack exchange,提问作者Peter Nyman
相关产品推荐
相关产品推荐

