在 Python 中将 3D 数组保存到一堆 2D 图像中
问题描述
我制作了一个由数字(0~4)组成的 3D 数组.我想要的是将 3D 数组保存为 2D 图像堆栈(如果可能,保存 *.tiff 文件).我该怎么办?
I made a 3D array, which consists of numbers(0~4). What I want is to save 3D array as a stack of 2D images(if possible, save *.tiff file). What am I supposed to do?
import numpy as np
a = np.random.randint(0,5, size=(100,100,100))
a = a.astype('int8')
解决方案
这应该可行.我还没有测试过它,但是我已经使用这种方法将彩色图像分成了 RGB 切片,并且它应该在这里以几乎相同的方式工作,假设您不想先对这些像素值做任何事情.(它们将非常接近图像中的相同颜色).
This should work. I haven't tested it but I have separated color images into RGB slices using this method and it should work pretty much the same way here, assuming you don't want to do anything with those pixel values first. (They will be very close to the same color in an image).
import imageio
import numpy as np
a = np.random.randint(0,5, size=(100,100,100))
a = a.astype('int8')
for i in range(100):
newimage = a[:, :, i]
imageio.imwrite("path/to/image%d.tiff" %i, newimage)
相关文章