Writing NRRD files

nrrd.write(file, data[, header, ...])

Write numpy.ndarray to NRRD file

nrrd.write(file: Union[str, IO], data: NDArray[Any, Any], header: Optional[Dict[str, Any]] = None, detached_header: bool = False, relative_data_path: bool = True, custom_field_map: Optional[Dict[str, Literal['int', 'double', 'string', 'int list', 'double list', 'string list', 'quoted string list', 'int vector', 'double vector', 'int matrix', 'double matrix']]] = None, compression_level: int = 9, index_order: Literal['F', 'C'] = 'F')[source]

Write numpy.ndarray to NRRD file

The file parameter specifies the absolute or relative filename to write the NRRD file to or an io.BytesIO object.

If file is a filename and the extension is .nhdr, then the detached_header parameter is set to true automatically. If the detached_header parameter is set to True and the filename ends in .nrrd, then the header file will have the same path and base name as the file but with an extension of .nhdr. In all other cases, the header and data are saved in the same file.

header is an optional parameter containing the fields and values to be added to the NRRD header.

Note

detached_header is ignored if file is io.BytesIO and not a str filename

Note

The following fields are automatically generated based on the data parameter ignoring these values in the header: ‘type’, ‘endian’, ‘dimension’, ‘sizes’, and ‘data file’. In addition, the generated fields will be added to the given header. Thus, one can check the generated fields by viewing the passed header.

Note

The default encoding field used if not specified in header is ‘gzip’.

Note

The index_order parameter must be consistent with the index order specified in read(). Reading an NRRD file in C-order and then writing as Fortran-order or vice versa will result in the data being transposed in the NRRD file.

See Writing NRRD files for more information on writing NRRD files.

Parameters
filestr or io.IOBase

Filename or file object of the NRRD file

datanumpy.ndarray

Data to save to the NRRD file

headerdict (str, Object), optional

NRRD headers

detached_headerbool or str, optional

Whether the header and data should be saved in separate files. Defaults to False. If a str is given this specifies the path to the datafile. This path will ONLY be used if the given filename ends with nhdr (i.e. the file is a header)

relative_data_pathbool

Whether the data filename in detached header is saved with a relative path or absolute path. This parameter is ignored if there is no detached header. Defaults to True

custom_field_mapdict (str, str), optional

Dictionary used for parsing custom field types where the key is the custom field name and the value is a string identifying datatype for the custom field.

compression_levelint

Integer between 1 and 9 specifying the compression level when using a compressed encoding (gzip or bzip). A value of 1 compresses the data the least amount and is the fastest, while a value of 9 compresses the data the most and is the slowest.

index_order{‘C’, ‘F’}, optional

Specifies the index order used for writing. Either ‘C’ (C-order) where the dimensions are ordered from slowest-varying to fastest-varying (e.g. (z, y, x)), or ‘F’ (Fortran-order) where the dimensions are ordered from fastest-varying to slowest-varying (e.g. (x, y, z)).