在 Python 中将 3D 数组保存到一堆 2D 图像中 [英] Save 3D array into a stack of 2D images in Python

查看:41
本文介绍了在 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 图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆