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

Class PILWriter

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

Write image data into files on disk using pillow. It's based on the Image module in PIL library: https://pillow.readthedocs.io/en/stable/reference/Image.html .. code-block:: python import numpy as np from monai.data import PILWriter np_data = np.arange(48

Source from the content-addressed store, hash-verified

673
674@require_pkg(pkg_name="PIL")
675class PILWriter(ImageWriter):
676 """
677 Write image data into files on disk using pillow.
678
679 It's based on the Image module in PIL library:
680 https://pillow.readthedocs.io/en/stable/reference/Image.html
681
682 .. code-block:: python
683
684 import numpy as np
685 from monai.data import PILWriter
686
687 np_data = np.arange(48).reshape(3, 4, 4)
688 writer = PILWriter(np.uint8)
689 writer.set_data_array(np_data, channel_dim=0)
690 writer.write("test1.png", verbose=True)
691 """
692
693 output_dtype: DtypeLike
694 channel_dim: int | None
695 scale: int | None
696
697 def __init__(
698 self, output_dtype: DtypeLike = np.float32, channel_dim: int | None = 0, scale: int | None = 255, **kwargs
699 ):
700 """
701 Args:
702 output_dtype: output data type.
703 channel_dim: channel dimension of the data array. Defaults to 0.
704 ``None`` indicates data without any channel dimension.
705 scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling
706 [0, 255] (uint8) or [0, 65535] (uint16). Default is None to disable scaling.
707 kwargs: keyword arguments passed to ``ImageWriter``.
708 """
709 super().__init__(output_dtype=output_dtype, channel_dim=channel_dim, scale=scale, **kwargs)
710
711 def set_data_array(
712 self,
713 data_array: NdarrayOrTensor,
714 channel_dim: int | None = 0,
715 squeeze_end_dims: bool = True,
716 contiguous: bool = False,
717 **kwargs,
718 ):
719 """
720 Convert ``data_array`` into 'channel-last' numpy ndarray.
721
722 Args:
723 data_array: input data array with the channel dimension specified by ``channel_dim``.
724 channel_dim: channel dimension of the data array. Defaults to 0.
725 ``None`` indicates data without any channel dimension.
726 squeeze_end_dims: if ``True``, any trailing singleton dimensions will be removed.
727 contiguous: if ``True``, the data array will be converted to a contiguous array. Default is ``False``.
728 kwargs: keyword arguments passed to ``self.convert_to_channel_last``,
729 currently support ``spatial_ndim``, defaulting to ``2``.
730 """
731 self.data_obj = self.convert_to_channel_last(
732 data=data_array,

Callers 6

test_write_grayMethod · 0.90
test_write_rgbMethod · 0.90
test_write_2channelsMethod · 0.90

Calls

no outgoing calls

Tested by 6

test_write_grayMethod · 0.72
test_write_rgbMethod · 0.72
test_write_2channelsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…