依赖安装
pip install pynrrd
官方文档
https://pynrrd.readthedocs.io/en/stable/
常用操作
1. 读部分
nrrd.read()
nrrdpath = "your nrrd file path"
data, header = nrrd.read(nrrdpath)
# data为ndarray的数据类型, 查看data的size
print(data.shape)
nrrd.read_header()
等于nrrd.read()只取header部分
nrrdpath = "your nrrd file path"
header = nrrd.read_header(nrrdpath)
nrrd.read_data()
等于nrrd.read()只取data部分
nrrdpath = "your nrrd file path"
data = nrrd.read_data(nrrdpath)
2. 写部分
nrrd.write()
# 以下例子来自官方
# 写入data、标准nrrd header域数据和自定义私有域
data = np.linspace(1, 60, 60).reshape((3, 10, 2))
header = {'kinds': ['domain', 'domain', 'domain'], 'units': ['mm', 'mm', 'mm'], 'spacings': [1.0458, 1.0458, 2.5], 'space': 'right-anterior-superior', 'space directions': np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), 'encoding': 'ASCII', 'custom_field_here1': 24.34, 'custom_field_here2': np.array([1, 2, 3, 4])}
custom_field_map = {'custom_field_here1': 'double', 'custom_field_here2': 'int list'}
# data类型为ndarray
nrrd.write('output.nrrd', data, header, custom_field_map=custom_field_map)
该函数更多说明查看官方文档:https://pynrrd.readthedocs.io/en/stable/reference/writing.html