MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / create_backend_obj

Method create_backend_obj

monai/data/image_writer.py:822–862  ·  view source on GitHub ↗

Create a PIL object from ``data_array``. Args: data_array: input data array. dtype: output data type. scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling [0, 255] (uint8) or [0, 65535] (uint16). Default

(
        cls,
        data_array: NdarrayOrTensor,
        dtype: DtypeLike = None,
        scale: int | None = 255,
        reverse_indexing: bool = True,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

820
821 @classmethod
822 def create_backend_obj(
823 cls,
824 data_array: NdarrayOrTensor,
825 dtype: DtypeLike = None,
826 scale: int | None = 255,
827 reverse_indexing: bool = True,
828 **kwargs,
829 ):
830 """
831 Create a PIL object from ``data_array``.
832
833 Args:
834 data_array: input data array.
835 dtype: output data type.
836 scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling
837 [0, 255] (uint8) or [0, 65535] (uint16). Default is None to disable scaling.
838 reverse_indexing: if ``True``, the data array's first two dimensions will be swapped.
839 kwargs: keyword arguments. Currently ``PILImage.fromarray`` will read
840 ``image_mode`` from this dictionary, defaults to ``None``.
841
842 See also:
843
844 - https://pillow.readthedocs.io/en/stable/reference/Image.html
845 """
846 data: np.ndarray = super().create_backend_obj(data_array)
847 if scale:
848 # scale the data to be in an integer range
849 data = np.clip(data, 0.0, 1.0) # png writer only can scale data in range [0, 1]
850
851 if scale == np.iinfo(np.uint8).max:
852 data = (scale * data).astype(np.uint8, copy=False)
853 elif scale == np.iinfo(np.uint16).max:
854 data = (scale * data).astype(np.uint16, copy=False)
855 else:
856 raise ValueError(f"Unsupported scale: {scale}, available options are [255, 65535].")
857 if dtype is not None:
858 data = data.astype(get_equivalent_dtype(dtype, np.ndarray), copy=False)
859 if reverse_indexing:
860 data = np.moveaxis(data, 0, 1)
861
862 return PILImage.fromarray(data, mode=kwargs.pop("image_mode", None))
863
864
865def init():

Callers 1

writeMethod · 0.95

Calls 4

get_equivalent_dtypeFunction · 0.90
astypeMethod · 0.80
popMethod · 0.80
create_backend_objMethod · 0.45

Tested by

no test coverage detected