遍历 numpy 数组以将它们保存为 tif 文件

Loop over numpy array to save them as tif files

我正在尝试使用 for 循环将形状为 (625, 256, 256, 4) 的 numpy 数组 (train_images) 保存为文件夹中的 tif 图像。即 625 张 256 x 256 像素的 RGBN 图像。目前我的代码如下所示:

path = str(os.getcwd) + "/data/train_images"

for i in train_images:
    num = 0
    i.save(num + '.tif')
    num +=1

但是,无法像这样将 numpy 数组保存为 tif 文件。最后,我想在名为 0.tif、1.tif 等

的文件夹中保存 625 张 (RGBN) tif 图像

scipy.misc中尝试imsave,如下:

path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
    import scipy.misc
    scipy.misc.imsave(path + str(num)  + '.tif', img)
    num +=1

要从文件中读取图像,您需要使用以下命令:

import imageio
im = imageio.imread('0.tif')

希望对您有所帮助。