ImageIO#
- class petpal.utils.image_io.ImageIO(verbose: bool = True)#
ImageIOto handle reading and writing imaging data and metadata.Provides several tools designed for reading and writing data within the Python environment.
- Key methods include:
save_nii(): Saves a loaded NIfTI file to a file path.extract_image_from_nii_as_numpy(): Extracts imaging data from a NIfTI file as a numpy array.extract_header_from_nii(): Extracts header information from a NIfTI file as a dictionary.extract_np_to_nibabel(): Wraps imaging information in numpy into an Nibabel image.
- Variables:
verbose (bool) – Set to True to output processing information.
Initializes
ImageIOand sets verbose.- Parameters:
verbose (bool) – Set to True to print debugging info to shell. Defaults to True.
- save_nii(image: nibabel.nifti1.Nifti1Image, out_file: str)#
Wrapper to save nifti to file.
- Parameters:
image (nibabel.nifti1.Nifti1Image) – Nibabel-type image to write to file.
out_file (str) – File path to which image will be written.
- extract_image_from_nii_as_numpy(image: nibabel.nifti1.Nifti1Image) numpy.ndarray#
Convenient wrapper to extract data from a .nii or .nii.gz file as a numpy array.
- Parameters:
image (nibabel.nifti1.Nifti1Image) – Nibabel-type image to write to file.
- Returns:
The data contained in the .nii or .nii.gz file as a numpy array.
- extract_header_from_nii(image: nibabel.nifti1.Nifti1Image) nibabel.filebasedimages.FileBasedHeader#
Convenient wrapper to extract header information from a .nii or .nii.gz file as a nibabel file-based header.
- Parameters:
image (nibabel.nifti1.Nifti1Image) – Nibabel-type image to write to file.
- Returns:
image_header (FileBasedHeader) – The nifti header.
- extract_np_to_nibabel(image_array: numpy.ndarray, header: nibabel.filebasedimages.FileBasedHeader, affine: numpy.ndarray) nibabel.nifti1.Nifti1Image#
Wrapper to convert an image array into nibabel object.
- Parameters:
image_array (np.ndarray) – Array containing image data.
header (FileBasedHeader) – Header information to include.
affine (np.ndarray) – Affine information we need to keep when rewriting image.
- Returns:
image_nibabel (nibabel.nifti1.Nifti1Image) – Image stored in nifti-like nibabel format.
- static affine_parse(image_affine: numpy.ndarray) tuple#
Parse the components of an image affine to return origin, spacing, direction.
- Parameters:
image_affine (np.ndarray) – A 4x4 affine matrix defining spacing, origin, and direction of an image.
- extract_np_to_ants(image_array: numpy.ndarray, affine: numpy.ndarray) ants.ANTsImage#
Wrapper to convert an image array into ants object. Note header info is lost as ANTs does not carry this metadata.
- Parameters:
image_array (np.ndarray) – Array containing image data.
affine (np.ndarray) – Affine information we need to keep when rewriting image.
- Returns:
image_ants (ants.ANTsImage) – Image stored in nifti-like nibabel format.