Python 追加NumPy数组时,列表将失去形状

Python 追加NumPy数组时,列表将失去形状,python,arrays,numpy,Python,Arrays,Numpy,我使用PIL加载图像,然后将它们转换为NumPy数组。然后我必须基于图像列表创建一个新图像,因此我将所有数组附加到一个列表中,然后将列表转换回一个数组,因此图像列表的形状有4个维度(n_图像、高度、宽度、rgb_通道)。我正在使用以下代码: def gallery(array, ncols=4): nindex, height, width, intensity = array.shape nrows = nindex // ncols # want result.sh

我使用PIL加载图像,然后将它们转换为NumPy数组。然后我必须基于图像列表创建一个新图像,因此我将所有数组附加到一个列表中,然后将列表转换回一个数组,因此图像列表的形状有4个维度(n_图像、高度、宽度、rgb_通道)。我正在使用以下代码:

def gallery(array, ncols=4):

    nindex, height, width, intensity = array.shape
    nrows = nindex // ncols
    # want result.shape = (height*nrows, width*ncols, intensity)

    result = (array.reshape(nrows, ncols, height, width, intensity)
              .swapaxes(1,2)
              .reshape(height*nrows, width*ncols, intensity))
    return result

def make_array(dim_x):
        for i in range(dim_x):
           print('series',i) 
           series = []
           for j in range(TIME_STEP-1):
              print('photo',j)
              aux = np.asarray(Image.open(dirpath+'/images/pre_images /series_{0}_Xquakemap_{1}.jpg'.format(i,j)).convert('RGB'))
              print(np.shape(aux))
              series.append(aux)
              print(np.shape(series))

        im = Image.fromarray(gallery(np.array(series)))
        im.save(dirpath+'/images/gallery/series_{0}_Xquakemap.jpg'.format(i))
        im_shape = (im.size)

make_array(n_photos)
# n_photos is the total of photos in the dirpath
问题是当
系列
列表上的追加发生时,图像的形状(添加的NumPy数组)丢失。因此,当尝试在函数
库中重塑数组时,会导致问题。上面代码的输出片段如下:

...

series 2
photo 0
(585, 619, 3)
(1, 585, 619, 3)
photo 1
(587, 621, 3)
(2,)
photo 2
(587, 621, 3)
(3,)
photo 3
(587, 621, 3)
(4,)
...
如您所见,在附加第二张照片时,列表将丢失一个维度。这很奇怪,因为代码在前两次迭代中工作,这两次迭代使用的图像相当相同。我尝试使用
np.stack()
,但错误仍然存在

我在Github上也发现了这一点,但我认为它不适用于这种情况,即使行为类似

在Ubuntu 18、Python 3.7.3和Numpy 1.16.2上工作


编辑:在第二个函数中添加了@kwinkunks询问的内容

,我认为您需要将
系列=[]
移动到外循环之前。

这是我对问题的再现:

import numpy as np
from PIL import Image

TIME_STEP = 3

def gallery(array, ncols=4):
    """Stitch images together."""
    nindex, height, width, intensity = array.shape
    nrows = nindex // ncols
    result = array.reshape(nrows, ncols, height, width, intensity)
    result = result.swapaxes(1,2)
    result = result.reshape(height*nrows, width*ncols, intensity)
    return result

def make_array(dim_x):
    """Make an image from a list of arrays."""
    series = []  # <<<<<<<<<<< This is the line you need to check.
    for i in range(dim_x):
        for j in range(TIME_STEP - 1):
            aux = np.ones((100, 100, 3)) * np.random.randint(0, 256, 3)
            series.append(aux.astype(np.uint8))

    im = Image.fromarray(gallery(np.array(series)))

    return im

make_array(4)
将numpy导入为np
从PIL导入图像
时间步长=3
def库(阵列,ncols=4):
“”“将图像缝合在一起。”“”
九度,高度,宽度,强度=数组.shape
nrows=nindex//ncols
结果=阵列。重塑(nrows、ncols、高度、宽度、强度)
结果=结果交换(1,2)
结果=结果。重塑(高度*nrows,宽度*ncols,强度)
返回结果
def make_阵列(dim_x):
“”“从数组列表生成图像。”“”

series=[]#很难说出你想做什么。你的函数不会返回任何东西,你也不会调用它。也许你可以做一个完整的,可运行的例子?(例如,使用随机图像数据)我的问题是:为什么要将数组转换为列表?函数
gallery()
的作用是什么?一个系列中的所有图像都有相同的形状吗?我编辑了这些问题以添加您所问的内容。回答你的问题:1。我这样做是因为我链接的问题说这是一个解决办法,但不管有没有这个问题,问题都会不断显现出来。2. <代码>图库
使用图像列表创建网格3。是的,所有的图像都有相同的形状。在输出中,第一个元组显示图像形状
print(np.shape(aux))
。您正在为
范围(dim_x)
中的每个步骤制作一个新的
系列
,因此您只会得到您制作的最后一个;也许你想在外环内部进行成像步骤?这将有助于看到输入和预期输出的例子。如果能有一个健康的家庭,那真的会很有帮助。FWIW,我的建议是不要在一个大数组中管理一切,交换轴和改变形状等,而是管理图像列表(比如)。换言之,“保持简单。”我试着回答,因为我认为
series=[]
放错了地方。我希望它至少朝着正确的方向发展!就这样!我遇到的另一个问题是,其中一个图像的大小因其他功能而改变。谢谢