python 将文件夹内所有2D图像堆叠为3D图像并保存

python 将文件夹内所有2D图像堆叠为3D图像并保存

导入必要的库:

from skimage import io

创建一个空的3维数组:

np.zeros(shape=(3,4,5), dtype='float64')

用2维数组对3维数组的每一维赋值:

im3d[i]=im2d

代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sat Sep 22 09:58:22 2018


@author: sy
"""
import numpy as np
import os
from skimage import io


filepath='D:\\DATA'
files=os.listdir(filepath)
count=0
im3d=np.zeros(shape=(len(files),io.imread(os.path.join(filepath,files[0])).shape[0],
              io.imread(os.path.join(filepath,files[0])).shape[1]),
             dtype='uint16')


count=0
for file_ in files:
    im2d=io.imread(os.path.join(filepath,file_))
    im3d[count]=im2d
    count+=1


print(im3d.shape)
print(im3d.dtype)


io.imsave('D:\\3D\\1.tif',im3d)

你可能感兴趣的:(python)