首页 > 解决方案 > 使用 Python 读取大量图像数据并将其转换为 Numpy 数组

问题描述

我刚开始使用 Python 和 Numpy 数组。我正在阅读位于许多不同文件夹中的大量图像数据集。虽然,阅读图像时一切正常,但我不断收到一个错误,我不确定它到底是什么。我试图研究它,但不幸的是我没有得到真正的答案。请帮助我解决这个问题。

我的代码如下。

import os
import numpy as np
import matplotlib.pyplot as mpplot
import matplotlib.image as mpimg

images = []
path = "../compCarsThesisData/"

for root, _, files in os.walk(path):
    current_directory_path = os.path.abspath(root)

for f in files:
    name, ext = os.path.splitext(f)
        if ext == ".jpg":
           current_image_path = os.path.join(current_directory_path, f)
           current_image = mpimg.imread(current_image_path)
           images.append(current_image)
           print(files)

for img in images:
    print (img.shape)

我面临的错误也在下面。

 File "reading.py", line 15, in <module>
current_image = mpimg.imread(current_image_path)
File "C:\Users\zeele\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\image.py", line 1359, in imread with Image.open(fname) as image:
File "C:\Users\zeele\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2618, in open
prefix = fp.read(16)
OSError: [Errno 22] Invalid argument

PS 作为新成员,如果问题不清楚或不直接,请不要介意。您的帮助将不胜感激。谢谢。

标签: pythonnumpymatplotlib

解决方案


推荐阅读