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

查看:40
本文介绍了使用Python将3D数组保存到2D图像堆栈中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个3D数组,它由数字(0〜4)组成.我想要的是将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天全站免登陆