繁体   English   中英

无法将ndarrays列表转换为numpy数组

[英]Fail to convert List of ndarrays to numpy array

我正在以numpy ndarray的形式逐个读取数千张图像(所有三个通道),并将它们附加到列表中。 最后,我想将此列表转换为numpy数组:

import numpy as np
from PIL import Image

def read_image_path(path, img_size=227):
    img = Image.open(path)
    img = np.array(img.resize([img_size, img_size]))
    return img

我从如下字典中读取每个图像路径:

{1:{'img_path': 'path-to-image', 'someOtherKeys':'...'}, 2:{...}}

images = []
for key in key:
    img = read_image_path(dataset_dictionary[key]['img_path'])
    images.append(img)

到这里都很好。 我有一个大小为(227,227,3)的ndarray图像矩阵列表。 但是,当我尝试将“图像”转换为numpy数组并从函数返回它时,出现以下错误:

return np.array(images)

返回np.array(images)

ValueError:无法将输入数组从形状(227,227,3)广播到形状(227,227)

如有任何想法,我将不胜感激。

您最有可能拥有形状为(227,227)而不是(227,227,3)的img(或图像)。

以下代码应告诉您哪个图像是犯罪者。

for key in key:
    img = read_image_path(dataset_dictionary[key]['img_path'])
    if img.shape != (227,227,3):
            print(key)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM