niworkflows.utils.images module

Utilities to manipulate images.

niworkflows.utils.images.demean(in_file, in_mask, only_mask=False, newpath=None)[source]

Demean in_file within the mask defined by in_mask.

niworkflows.utils.images.dseg_label(in_seg, label, newpath=None)[source]

Extract a particular label from a discrete segmentation.

niworkflows.utils.images.nii_ones_like(in_file, value, dtype, newpath=None)[source]

Create a NIfTI file filled with value, matching properties of in_file.

niworkflows.utils.images.overwrite_header(img, fname)[source]

Rewrite file with only changes to the header

The data block is copied without scaling, avoiding copies in memory. The header is checked against the target file to ensure that no changes to the size, offset or interpretation of the data block will result.

This function will not respect calls to:

  • img.header.set_slope_inter()

  • img.header.set_data_shape()

  • img.header.set_data_offset()

These will all be determined by img.dataobj, which must be an ArrayProxy.

If the qform or sform are updated, the img.header.get_best_affine() method must match img.affine, or your changes may be lost.

The intended use of this method is for making small header fixups that do not change the data or affine, e.g.:

>>> import nibabel as nb
>>> img = nb.load(nifti_fname, mmap=False)
>>> img.header.set_qform(*img.header.get_sform(coded=True))
>>> img.header['descrip'] = b'Modified with some extremely finicky tooling'
>>> overwrite_header(img, nifti_fname)

This is a destructive operation, and the image object should be considered unusable after calling this function.

This should only be called with an image loaded with mmap=False, or else you risk getting a BusError.

niworkflows.utils.images.resample_by_spacing(in_file, zooms, order=3, clip=True, smooth=False)[source]

Regrid the input image to match the new zooms.

niworkflows.utils.images.rotate_affine(img, rot=None)[source]

Rewrite the affine of a spatial image.

niworkflows.utils.images.rotation2canonical(img)[source]

Calculate the rotation w.r.t. cardinal axes of input image.

niworkflows.utils.images.set_consumables(header, dataobj)[source]
niworkflows.utils.images.unsafe_write_nifti_header_and_data(fname, header, data)[source]

Write header and data without any consistency checks or data munging

This is almost always a bad idea, and you should not use this function without a battery of tests for your specific use case.

If you’re not using this for NIfTI files specifically, you’re playing with Fortran-ordered fire.

niworkflows.utils.images.update_header_fields(fname, **kwargs)[source]

Adjust header fields