简体   繁体   English

添加2d数组以在python中制作3d

[英]Add 2d array to make 3d in python

I have a requirement to read image files( 28*28) from a folder and stack them together to make a single array for analysis. 我需要从文件夹中读取图像文件(28 * 28)并将它们堆叠在一起以形成单个阵列进行分析。

I have the following code: 我有以下代码:

for fname in os.listdir(dirname):
    im = Image.open(os.path.join(dirname, fname))
    imarray = np.array(im)
    final = np.stack((final,imarray ), axis = 0)

am getting the following error: ValueError: all input arrays must have the same shape 正在收到以下错误:ValueError:所有输入数组必须具有相同的形状

imarray is (28,28) and i have 60K images in that folder so i want to make a array of size (60000,28,28) imarray是(28,28)并且我在那个文件夹中有60K图像,所以我想制作一个大小为(60000,28,28)的数组

Thanks for the help NK 感谢您的帮助NK

Build a list of all components and stack them once: 建立所有组件的列表,并将它们堆叠一次:

alist = []
for fname in os.listdir(dirname):
    im = Image.open(os.path.join(dirname, fname))
    imarray = np.array(im)
    alist.append(imarray)
final = np.stack(alist)   # axis=0 is the default

This will join them on a new initial axis. 这将使它们加入新的初始轴。

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

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