hdr转 img

import nibabel as nb
fname = '.img'  
img = nb.load(fname)
nb.save(img, fname.replace('.img', '.nii'))
import os
import shutil
import nibabel as nb

path1 = r"C:\Users\tangx\Desktop\6"
path2 = r"C:\Users\tangx\Desktop\12"

savepath1 = r"C:\Users\tangx\Desktop\six"
savepath2 = r"C:\Users\tangx\Desktop\twelve"

def img2nii(p1, p2):
    img = nb.load(p1)
    nb.save(img, p2)




f1 = os.listdir(path1)
f2 = os.listdir(path2)

for i in f2:


    newpath = os.path.join(savepath1,i)  # 新文件夹路径 "C:\Users\tangx\Desktop\six\IBIS0015"
    if not os.path.exists(newpath):
        os.makedirs(newpath) # 创建新文件夹路径

    name1 = "{}-T1-reorient-n3.img".format(i)  # 文件名
    name2 = "{}-T2-reorient-n3-alignT1.img".format(i)
    name3 = "skull-final.img"

    fname1 = os.path.join(path1, str(i), name1)  # 文件路径 "C:\Users\tangx\Desktop\six\IBIS0015\{}-T1-reorient-n3.img"
    fname2 = os.path.join(path1,str(i),name2)
    fname3 = os.path.join(path1,str(i),name3)

    savename1 = os.path.join(newpath,name1.replace(".img",".nii.gz"))
    savename2 = os.path.join(newpath,name2.replace(".img",".nii.gz"))
    savename3 = os.path.join(newpath,name3.replace(".img",".nii.gz"))

    img2nii(fname1,savename1)
    img2nii(fname2,savename2)
    img2nii(fname3,savename3)







你可能感兴趣的:(python,python)