一次将所有切片转换为 nii.gz 的问题

Problems with converting all slices into nii.gz at once

我正在使用以下代码将我所有的 332 npy 切片转换为 nii.gz 格式:

import numpy as np
import nibabel as nib

file_dir = "D:/Volumes convertidos LIDC/"
fileNPY1 = "slice200.npy"
img_array1 = np.load(file_dir + fileNPY1) 
nifti_file = nib.Nifti1Image(img_array1 , np.eye(4))
nib.save(nifti_file, "D:/slices convertidos/slice200converted.nii.gz")

我有太多切片(和大量图像)无法继续这样做,有没有办法一次转换它们?

我不认识 nibabel,我不确定我是否理解正确,但也许这会有所帮助:

import numpy as np
import nibabel as nib

file_dir = "D:/Volumes convertidos LIDC/"
for i in range(332):
    fileNPY1 = f"slice{i}.npy"
    img_array1 = np.load(file_dir + fileNPY1) 
    nifti_file = nib.Nifti1Image(img_array1 , np.eye(4))
    nib.save(nifti_file, f"D:/slicesconvertidos/slice{i}converted.nii.gz")