import SimpleITK as sitk
import numpy as np
import os
from batchgenerators.utilities.file_and_folder_operations import *
def nii2mhd(file_path):
itk_img = sitk.ReadImage(file_path)
save_path = file_path.strip(’.gz’).strip(’.nii’)+’.mhd’
sitk.WriteImage(itk_img, save_path)
print(‘convert success:’+save_path)
def mhd2nii(file_path):
itk_img = sitk.ReadImage(file_path)
save_path = file_path.strip(’.mhd’)+ ‘.nii.gz’
sitk.WriteImage(itk_img, save_path)
print(‘convert success:’ + save_path)
//批量处理,将文件夹下的所有nii文件转换为mhd文件
if name==“main”:
root = “F:\…”
imgdir = os.listdir(root)
for i in imgdir:
a =i.split(".nii")[0]
imgpath = join(root, a + “.nii.gz” )
nii2mhd(imgpath)