我正在尝试遍历.png文件的文件夹并对其进行OCR。迭代有效,但是当我尝试使用PIL打开图像时,就会出现错误。
import pytesseract
from PIL import Image
import os
for filename in os.listdir('C:/Users/Artur/Desktop/Sequenz_1'):
if filename.endswith('.png'):
print(filename)
这很好用。它将打印文件夹中的每个.png文件名。但是当我尝试OCR时:
import pytesseract
from PIL import Image
import os
for filename in os.listdir('C:/Users/Artur/Desktop/Sequenz_1'):
if filename.endswith('.png'):
print(pytesseract.image_to_string(Image.open(filename)))
输出:
Traceback (most recent call last):
File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 8, 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: 'frame_0000.png'
编辑:
感谢Benehiko,现在效果很好。
代码:
import pytesseract
from PIL import Image
import glob
images = glob.glob('C:/Users/Artur/Desktop/Sequenz_1/*.png')
for image in images:
with open(image, 'rb') as file:
img = Image.open(file)
print(pytesseract.image_to_string(img))
答案 0 :(得分:2)
我有一个Python脚本,使用glob从文件夹中打开jpg图片。只需将“ .jpg”更改为“ .png”
,即可打开png。我使用的代码如下:
import glob
from PIL import Image
images = glob.glob("Images/*.jpg")
for image in images:
with open(image, 'rb') as file:
img = Image.open(file)
img.show()