简体   繁体   English

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

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

I am reading thousands of images (all three channels), one by one, in form of a numpy ndarray and append them to a list. 我正在以numpy ndarray的形式逐个读取数千张图像(所有三个通道),并将它们附加到列表中。 At the end I want to convert this list into a numpy array: 最后,我想将此列表转换为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

I read each image path from a dictionary that looks like: 我从如下字典中读取每个图像路径:

{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)

Up to here it's all fine. 到这里都很好。 I have a list of ndarray image matrices of size (227,227,3). 我有一个大小为(227,227,3)的ndarray图像矩阵列表。 But when I try to convert "images" to numpy array and return it from the function, it gives the following error: 但是,当我尝试将“图像”转换为numpy数组并从函数返回它时,出现以下错误:

return np.array(images)

return np.array(images) 返回np.array(images)

ValueError: could not broadcast input array from shape (227,227,3) into shape (227,227) ValueError:无法将输入数组从形状(227,227,3)广播到形状(227,227)

I will be grateful to have anyone's idea about this. 如有任何想法,我将不胜感激。

Most likely you have a img (or images) which has the shape of (227,227) instead of (227,227,3). 您最有可能拥有形状为(227,227)而不是(227,227,3)的img(或图像)。

The following code should tell you which image is the offender. 以下代码应告诉您哪个图像是犯罪者。

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