Save 3D array into a stack of 2D images in Python(在 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)
这篇关于在 Python 中将 3D 数组保存到一堆 2D 图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中将 3D 数组保存到一堆 2D 图像中


基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 求两个直方图的卷积 2022-01-01