Python ';列表';对象没有属性';阅读';在PDF2图像中面对这个错误

Python ';列表';对象没有属性';阅读';在PDF2图像中面对这个错误,python,image,pdf,processing,Python,Image,Pdf,Processing,我有这个密码 tex=pytesseract.image_to_string(Image.open(pdf2image.convert_from_path(PDF_PATH)),lang='mar') 我想做这样的事情 tex=pytesseract.image_to_string(Image.open(image_path),lang='mar') 代码 错误 回溯(最近一次呼叫最后一次): 文件“D:\System\p\Python\lib\site packages\PIL\Image

我有这个密码

tex=pytesseract.image_to_string(Image.open(pdf2image.convert_from_path(PDF_PATH)),lang='mar')
我想做这样的事情

tex=pytesseract.image_to_string(Image.open(image_path),lang='mar')
代码

错误

回溯(最近一次呼叫最后一次):
文件“D:\System\p\Python\lib\site packages\PIL\Image.py”,第2882行,处于打开状态
fp.seek(0)
AttributeError:“list”对象没有属性“seek”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“D:\ocr.py”,第12行,在
tex=pytesseract.image_to_字符串(image.open(pdf2image.convert_from_path(PDF_path)),lang='mar')
文件“D:\System\p\Python\lib\site packages\PIL\Image.py”,第2884行,处于打开状态
fp=io.BytesIO(fp.read())
AttributeError:“list”对象没有属性“read”
该行

    pdf2image.convert_from_path(PDF_PATH)
返回图像列表,每个页面一个。pdf2image项目说明()说明:

其中图像是代表PDF文档每一页的PIL图像列表

解决方案

PIL函数Image.open()需要图像,而不是列表。因此,您可以做以下两件事之一:

  • 循环遍历convert_from_path()方法返回的列表,并将每个列表项(读取:每个图像)传递给pytesseract.image_to_string()
  • 如果确定pdf只包含一个页面,只需访问convert_from_path()方法返回的列表的第一个索引即可

  • pdf2image.convert_from_path
    返回一个
    列表
    ,该列表包含文档每页的图像对象。想要的行为是什么,你想迭代每个图像还是只拍摄第一个图像?在这成为一个可行的问题之前,你还有一些分析工作要做。请提供预期的[最小、可复制的示例]()。显示中间结果与您预期的不同之处。特别是,track
    fp
    ——您显然希望它是一个列表,但您的代码为它分配了一个列表。这种差异是如何产生的?你试图在一行中做太多的事情。将错误行分成不同的部分,然后您应该能够检测出导致错误的确切命令是什么。
    Traceback (most recent call last):
      File "D:\System\p\Python\lib\site-packages\PIL\Image.py", line 2882, in open
        fp.seek(0)
    AttributeError: 'list' object has no attribute 'seek'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "D:\ocr.py", line 12, in <module>
        tex=pytesseract.image_to_string(Image.open(pdf2image.convert_from_path(PDF_PATH)),lang='mar')
      File "D:\System\p\Python\lib\site-packages\PIL\Image.py", line 2884, in open
        fp = io.BytesIO(fp.read())
    AttributeError: 'list' object has no attribute 'read'
    
        pdf2image.convert_from_path(PDF_PATH)
    
        images = convert_from_path('/home/belval/example.pdf')