MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / SaveImage

Class SaveImage

monai/transforms/io/array.py:308–524  ·  view source on GitHub ↗

Save the image (in the form of torch tensor or numpy ndarray) and metadata dictionary into files. The name of saved file will be `{input_image_name}_{output_postfix}{output_ext}`, where the `input_image_name` is extracted from the provided metadata dictionary. If no metadata provid

Source from the content-addressed store, hash-verified

306
307
308class SaveImage(Transform):
309 """
310 Save the image (in the form of torch tensor or numpy ndarray) and metadata dictionary into files.
311
312 The name of saved file will be `{input_image_name}_{output_postfix}{output_ext}`,
313 where the `input_image_name` is extracted from the provided metadata dictionary.
314 If no metadata provided, a running index starting from 0 will be used as the filename prefix.
315
316 Args:
317 output_dir: output image directory.
318 Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
319 output_postfix: a string appended to all output file names, default to `trans`.
320 Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
321 output_ext: output file extension name.
322 Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
323 output_dtype: data type (if not None) for saving data. Defaults to ``np.float32``.
324 resample: whether to resample image (if needed) before saving the data array,
325 based on the ``"spatial_shape"`` (and ``"original_affine"``) from metadata.
326 mode: This option is used when ``resample=True``. Defaults to ``"nearest"``.
327 Depending on the writers, the possible options are
328
329 - {``"bilinear"``, ``"nearest"``, ``"bicubic"``}.
330 See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
331 - {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}.
332 See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
333
334 padding_mode: This option is used when ``resample = True``. Defaults to ``"border"``.
335 Possible options are {``"zeros"``, ``"border"``, ``"reflection"``}
336 See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
337 scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling
338 [0, 255] (``uint8``) or [0, 65535] (``uint16``). Default is ``None`` (no scaling).
339 dtype: data type during resampling computation. Defaults to ``np.float64`` for best precision.
340 if ``None``, use the data type of input data. To set the output data type, use ``output_dtype``.
341 squeeze_end_dims: if ``True``, any trailing singleton dimensions will be removed (after the channel
342 has been moved to the end). So if input is (C,H,W,D), this will be altered to (H,W,D,C), and
343 then if C==1, it will be saved as (H,W,D). If D is also 1, it will be saved as (H,W). If ``False``,
344 image will always be saved as (H,W,D,C).
345 data_root_dir: if not empty, it specifies the beginning parts of the input file's
346 absolute path. It's used to compute ``input_file_rel_path``, the relative path to the file from
347 ``data_root_dir`` to preserve folder structure when saving in case there are files in different
348 folders with the same file names. For example, with the following inputs:
349
350 - input_file_name: ``/foo/bar/test1/image.nii``
351 - output_postfix: ``seg``
352 - output_ext: ``.nii.gz``
353 - output_dir: ``/output``
354 - data_root_dir: ``/foo/bar``
355
356 The output will be: ``/output/test1/image/image_seg.nii.gz``
357
358 Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
359 separate_folder: whether to save every file in a separate folder. For example: for the input filename
360 ``image.nii``, postfix ``seg`` and ``folder_path`` ``output``, if ``separate_folder=True``, it will be
361 saved as: ``output/image/image_seg.nii``, if ``False``, saving as ``output/image_seg.nii``.
362 Default to ``True``.
363 Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
364 print_log: whether to print logs when saving. Default to ``True``.
365 output_format: an optional string of filename extension to specify the output image writer.

Callers 11

__init__Method · 0.90
run_testFunction · 0.90
run_inference_testFunction · 0.90
run_inference_testFunction · 0.90
create_transformFunction · 0.90
nifti_rwMethod · 0.90
png_rwMethod · 0.90
nrrd_rwMethod · 0.90
test_saved_contentMethod · 0.90
test_no_nameMethod · 0.90

Calls

no outgoing calls

Tested by 10

run_testFunction · 0.72
run_inference_testFunction · 0.72
run_inference_testFunction · 0.72
create_transformFunction · 0.72
nifti_rwMethod · 0.72
png_rwMethod · 0.72
nrrd_rwMethod · 0.72
test_saved_contentMethod · 0.72
test_no_nameMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…