python - 名称错误:python中的图像到文本错误
问题描述
我正在开发代码以使用以下代码将图像转换为文本。我在执行代码时看到以下错误。我真的不明白是什么导致了这个问题。任何人都可以帮助我确定问题。
from PIL import Image
import PIL.Image
from pytesseract import image_to_string
import pytesseract
img = Image.open('C:\\Users\\Documents\\convert_image_to_text\\Sample.png')
pytesseract.pytesseract.tesseract_cmd = 'C:\AppData\Local\Tesseract-OCR\tesseract.exe'
text = pytesseract.image_to_string('C:\\Users\\Documents\\convert_image_to_text\\Sample.png')
print(text)
以下是错误:
File "C:/Users/Documents/convert_image_to_text/program_to_convert_image_to_text.py", line 21, in <module>
text=image_to_string(img)
NameError: name 'img' is not defined
解决方案
您应该传递img
对象而不是路径
text = pytesseract.image_to_string(img)
推荐阅读
- c++ - 复制对象数组
- xml - 父项下特定标记的 XPath
- ruby-on-rails - 尝试在 RoR 应用程序中实现 simple_calendar 时出现 NoMethodError
- ios - 程序化 UITableView 单元格中出现不同宽度的图像
- typescript - 如何在本地项目中为 javascript 库创建类型定义?
- python - 尝试为 RNN 重用权重时出错
- css - 字体大小在移动 Safari 上的 DIV 中出现大于相同大小的数字
- html - 打开标志性引导程序不起作用
- r - 离散化 R 中的一列
- google-bigquery - 大查询 pars_timestamp 问题