【问题标题】:List loses shape when appending a NumPy array附加 NumPy 数组时列表丢失形状
【发布时间】:2019-09-30 23:36:59
【问题描述】:

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

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

问题是当series 列表上的追加发生时,图像的形状(添加的 NumPy 数组)会丢失。因此,当尝试在函数gallery 中重塑数组时,会出现问题。上面代码的输出的 sn-p 是这个:

...

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 上找到了这个 issue,但我认为它不适用于这种情况,即使行为相似。

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

编辑:添加了@kwinkunks 提出的问题

【问题讨论】:

  • 很难说你想做什么。你的函数不返回任何东西,你永远不会调用它。也许您可以制作一个完整的、可运行的示例? (例如,使用随机的类似图像的数据。)我有问题:为什么要将数组转换为列表?函数gallery() 有什么作用?一个系列中的所有图像都具有相同的形状吗?
  • 我编辑了问题以添加您提出的问题。回答您的问题: 1. 我这样做是因为我链接的问题说这是一种解决方法,但是无论有没有这种情况,问题都会不断出现。 2.gallery 使用图像列表创建一个网格 3. 是的,所有图像都具有相同的形状。在输出中,第一个元组显示图像形状print(np.shape(aux)),第二个元组显示系列形状print(np.shape(series))
  • 照片 0 的形状与其他照片不同。
  • 还是有点不清楚。你正在为range(dim_x) 中的每一步创建一个新的series,所以你只会得到你所做的最后一个;也许您想在外循环内进行图像形成步骤?查看输入和预期输出的示例将有所帮助。拥有minimal, reproducible example 真的很有帮助。 FWIW,我的建议是不要管理大型 ndarray 中的所有内容,交换轴和更改形状等,而是管理图像列表(例如)。换句话说,“保持简单”。
  • 我试着回答一下,因为我认为series = [] 放错了地方。我希望它至少朝着正确的方向发展!

标签: python arrays numpy


【解决方案1】:

在第二个函数中,我认为您需要将series = [] 移到外循环之前。

这是我对问题的再现:

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)

这会导致:

【讨论】:

  • 就是这样!我遇到的另一个问题是其中一个图像的大小被其他功能改变了。谢谢
猜你喜欢
  • 2021-10-19
  • 2019-05-18
  • 2019-08-26
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多