import json
import SimpleITK as sitk
image_path = "folder/image.nii.gz" # change this with your file
# read image
itk_image = sitk.ReadImage(image_path)
# get metadata dict
header = {k: itk_image.GetMetaData(k) for k in itk_image.GetMetaDataKeys()}
# save dict in 'header.json'
with open("header.json", "w") as outfile:
json.dump(header, outfile, indent=4)
对于单个文件夹中的每个图像:
import json
from pathlib import Path
import SimpleITK as sitk
# get list of .nii.gz files inside the given folder
folder = Path("folder") # change this
image_paths = filter(lambda f: str(f).endswith(".nii.gz"), folder.iterdir())
for image_path in image_paths:
# read image
itk_image = sitk.ReadImage(image_path)
# get metadata dict
header = {k: itk_image.GetMetaData(k) for k in itk_image.GetMetaDataKeys()}
# create unique name for header file
header_json_file = f"{image_path.name.replace('.nii.gz','')}_header.json"
# save dict in header file
with open(header_json_file, "w") as outfile:
json.dump(header, outfile, indent=4)
2条答案
按热度按时间wn9m85ua1#
可以使用SimpleITK读取Nifti文件的元数据字典,然后使用python的JSON library将其写出。
对于单个图像:
对于单个文件夹中的每个图像:
另请参阅this SimpleITK example
rjjhvcjd2#