Python OCR文件夹中的每个.png文件

Python OCR文件夹中的每个.png文件,python,ocr,Python,Ocr,我想遍历文件夹中的每个.png文件,并打印图像中包含的所有文本。第一次迭代工作正常,但第二次迭代出现错误 代码: 输出: 回溯(最近一次调用上次):文件 “C:\Users\Artur\Desktop\Pytesseract\u test.py”,中的第9行 Image=Image.open(filename)AttributeError:'PngImageFile'对象没有属性'open' “PngImageFile”对象没有“open”属性是什么意思?不是Image=Image.open(f

我想遍历文件夹中的每个.png文件,并打印图像中包含的所有文本。第一次迭代工作正常,但第二次迭代出现错误

代码:

输出:

回溯(最近一次调用上次):文件 “C:\Users\Artur\Desktop\Pytesseract\u test.py”,中的第9行 Image=Image.open(filename)AttributeError:'PngImageFile'对象没有属性'open'

“PngImageFile”对象没有“open”属性是什么意思?不是
Image=Image.open(filename)
就是这样做的吗

提前谢谢

编辑:

初始PngError已解决,但现在PIL库发生另一个错误:

import pytesseract
from PIL import Image
import os

directory = (r'C:\folder...')

for filename in os.listdir(directory):
    if filename.endswith('.png'):
        img = Image.open(filename)
        im = pytesseract.image_to_string(img)
        print(im)
输出:(frame_0000.png的ocr正确,然后)

它工作得非常好,遍历每个文件,打印每个文件名

但当我这么做的时候:

for filename in os.listdir(r'folderpath...'):
    print(filename)
for filename in os.listdir(r'folderpath...'):
    print(filename)
    print(pytesseract.image_to_string(Image.open(filename)))
给出了一个错误:

Bewegung_UHF_Plots.m
Traceback (most recent call last):
  File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 19, in <module>
    print(pytesseract.image_to_string(Image.open(filename)))
  File "C:\Users\Artur\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2580, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Bewegung_UHF_Plots.m'
Bewegung_UHF_Plots.m
回溯(最近一次呼叫最后一次):
文件“C:\Users\Artur\Desktop\Pytesseract\u test.py”,第19行,在
打印(pytesseract.image_到_字符串(image.open(文件名)))
文件“C:\Users\Artur\AppData\Local\Programs\Python\36\lib\site packages\PIL\Image.py”,第2580行,处于打开状态
fp=内置的.open(文件名为“rb”)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“Bewegung_UHF_Plots.m”

将变量
Image
的名称更改为类似于
pic
picture
的其他名称。第一次通过创建对象
Image
,即
Image.open(filename)
,或者从文件“filename”上的“Image”类实现“open”方法。这就是您在每次迭代中想要的。不幸的是,您所做的是将“Image”类引导出来,以支持新的“Image”对象,因此在第二次传递中,
Image.open(filename)
中的“Image”并不是指预期的“Image”类,而是指您的“Image”对象

通过改变以下方式解决您的问题:

Image = Image.open(filename)
im = pytesseract.image_to_string(Image)
致:


谢谢,这很有帮助,但是发生了另一个错误。请查看编辑部分。您确定当前目录中有名为frame_0001.png的文件吗?是的。我没有告诉程序读取“frame_0001.png”。它试图自己阅读。因为它能识别它,但不管什么原因都看不懂。但是,第一次迭代是有效的(“frame_0000.png”由程序读取)。@ArturMüllerRomanov尝试此图像。open(open(“path/to/file”,“rb”))如果直接使用文件名打开文件,则可以使用一个或两个“open”函数打开文件。但它在迭代过程中不起作用。请参阅Edit2部分。在directory=(r'C:\ folder…')中使用双反斜杠。我从snap获得了tesseract,最好杀死它并使用pytesseract。。
Bewegung_UHF_Plots.m
Traceback (most recent call last):
  File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 19, in <module>
    print(pytesseract.image_to_string(Image.open(filename)))
  File "C:\Users\Artur\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2580, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Bewegung_UHF_Plots.m'
Image = Image.open(filename)
im = pytesseract.image_to_string(Image)
img = Image.open(filename)
im = pytesseract.image_to_string(img)