*ライブラリ
NumPy と imageio からインポートされたよく知られたライブラリがあります import NumPy as np import os import nibabel as nib import imageio // ここで、nift を png に変換するコードを記述する
メソッド nift(.nii) 画像を png 画像に変換しますdef nii_to_image(niifile): filenames = os.listdir(filepath) #nii フォルダを読み込む slice_trans = [] #filename は nii イメージのパス
for f in filenames:
#Start reading nii files
img_path = os.path.join(filepath, f)
img = nib.load(img_path) #read nii
img_fdata = img.get_fdata()
fname = f.replace('.nii','')
# Remove the nickname of nii
img_f_path = os.path.join(imgfile, fname)
#Create a folder corresponding to the image of nii
if not os.path.exists(img_f_path):
os.mkdir(img_f_path) #New folder
# to image
(x,y,z) = img.shape
for i in range(z): #x is the sequence of images
silce = img_fdata[i, :, :] #Select which direction the slice can be
imageio.imwrite(os.path.join(img_f_path,'{}.png'.format(i)), silce) #Save image
#main function where fill path was gived
主要
if __name__ == '__main__':
filepath = '/content/drive/MyDrive/sem 8/dataset/pr'
imgfile = '/content/drive/MyDrive/sem 8/dataset/propi'
nii_to_image(filepath)