Python 将三维阵列调整为二维

Python 将三维阵列调整为二维,python,numpy,Python,Numpy,我有一些代码看起来像这样 number_of_pairs = int(len(path_list) / 2) pairs_of_images = [np.zeros( (number_of_pairs, self.image_height, self.image_height, 1)) for i in range(2)] labels = np.zeros((number_of_pairs, 1)) size = 105,105 for

我有一些代码看起来像这样

    number_of_pairs = int(len(path_list) / 2)
    pairs_of_images = [np.zeros(
        (number_of_pairs, self.image_height, self.image_height, 1)) for i in range(2)]
    labels = np.zeros((number_of_pairs, 1))
    size = 105,105
    for pair in range(number_of_pairs):
        image = Image.open(path_list[pair * 2])
        image = image.resize((105,105))
        image = np.asarray(image).astype(np.float64)
        print("before resize is{}".format(image))

        pairs_of_images[0][pair, :, :, 0] = image
然而,我在哪里得到了一个错误

图像对[1][pair,:,:,0]=图像
ValueError:无法执行此操作 将输入阵列从形状(105105,4)广播到形状(105105)

有没有办法消除阵列的第三维空间?

替换

image = Image.open(path_list[pair * 2])


“L”模式将图像转换为单通道图像。

我建议您仔细查看从np.asarray()获得的确切信息。你要让它根据你传入的信息推断数据类型,你知道它实际上在做什么吗?你能依赖它吗?3d维度的可能副本可能会编码颜色。如果是这样的话,没有它,你只会有一个黑白图像。看起来成对设置为拍摄单通道图像,但你正在加载3通道图像。
image = Image.open(path_list[pair * 2]).convert('L')