名称错误:python中的图像到文本错误

名称错误:python中的图像到文本错误,python,pandas,spyder,python-tesseract,Python,Pandas,Spyder,Python Tesseract,我正在开发代码,使用下面的代码将图像转换为文本。我在执行代码时看到以下错误。我真的不明白是什么导致了这个问题。有谁能帮我确定这个问题吗 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') pyte

我正在开发代码,使用下面的代码将图像转换为文本。我在执行代码时看到以下错误。我真的不明白是什么导致了这个问题。有谁能帮我确定这个问题吗


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


文件“C:/Users/Documents/convert_image_to_text/program_to_convert_image_to_text.py”,第21行,in
文本=图像到字符串(img)
NameError:未定义名称“img”

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

text = pytesseract.image_to_string(img)

我已编辑文件路径以排除敏感信息。我想路径在这里并不重要。