During handling of the above exception, another exception occurred解决方案

During handling of the above exception, another exception occurred解决方案

前言

今天在写python读取图片中的内容的脚本的时候,常用的图像处理库包括Pillow和OpenCV。以下是使用Pillow库读取图片中的计算公式的示例代码:

from PIL import Image
import pytesseract

# 读取图片
img = Image.open('formula.png')

# 将图片转换为灰度图像
img = img.convert('L')

# 使用pytesseract识别图片中的文本
formula = pytesseract.image_to_string(img)

# 输出识别结果
print(formula)

遇到了如下问题:
在这里插入图片描述

解决方案

通过核对代码内容,文件名称和文件路径等没有问题,包括把代码放到Linux上面跑都没有问题。

最后发现错误的原因是:在Python2和Python3中,tesseract所在的路径没有作为环境路径变量添加到Windows中。

在使pytesseract在windows上运行的步骤:

  1. https://www.continuum.io/downloads安装Python2或Python3;
  2. http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe安装tesseract for windows;
  3. 将tesseract目录作为PATH环境变量添加到windows中;

最后再执行代码脚本:

from PIL import Image
import pytesseract

# 读取图片
img = Image.open('formula.png')

# 将图片转换为灰度图像
img = img.convert('L')

# 使用pytesseract识别图片中的文本
formula = pytesseract.image_to_string(img)

# 输出识别结果
print(formula)

成功了。。。
在这里插入图片描述
在这里插入图片描述

总结

无论什么时候不能忽视细节,常见的问题都是最简单、直接、基础的因素产生的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_沉溺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值