使用Pillow Image.open遍历文件夹 [英] Iterate through folder with Pillow Image.open
本文介绍了使用Pillow Image.open遍历文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遍历.png文件的文件夹并对其进行OCR.迭代有效,但是当我尝试使用PIL打开图像时,就会出现错误.
I am trying to iterate through a folder of .png files and OCR them. The iteration works but as soon as I try to open Images with PIL it gives errors.
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时:
This works just fine. It prints every .png filename in the folder. But when I try to 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,它现在可以正常工作了.
Thanks to Benehiko it works fine now.
代码:
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))
推荐答案
我有一个Python脚本,使用glob从文件夹中打开jpg图像.只需将" .jpg"更改为" .png"
I have a python script opening jpg images from a folder using glob. Opening png will be the same concept by just changing the ".jpg" to ".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()
这篇关于使用Pillow Image.open遍历文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文