首页 > 解决方案 > 名称错误: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

标签: pythonpandasspyderpython-tesseract

解决方案


您应该传递img对象而不是路径

text = pytesseract.image_to_string(img)

推荐阅读