imgutils.data.encode

rgb_encode

imgutils.data.encode.rgb_encode(image: str | PathLike | bytes | bytearray | BinaryIO | Image, order_: str = 'CHW', use_float: bool = True) ndarray[source]
Overview:

Encode image as rgb channels.

Parameters:
  • image – Image to be encoded.

  • order – Order of encoding, default is CHW.

  • use_float – Use float to represent the channels, default is True. np.uint8 will be used when false.

Returns:

Encoded rgb image.

Examples::
>>> from PIL import Image
>>> from imgutils.data import rgb_encode
>>>
>>> image = Image.open('custom_image.jpg')
>>> image
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1606x1870 at 0x7F9EC37389D0>
>>>
>>> data = rgb_encode(image)
>>> data.shape, data.dtype
((3, 1870, 1606), dtype('float32'))
>>> data = rgb_encode(image, order_='CHW')
>>> data.shape, data.dtype
((3, 1870, 1606), dtype('float32'))
>>> data = rgb_encode(image, order_='WHC')
>>> data.shape, data.dtype
((1606, 1870, 3), dtype('float32'))
>>> data = rgb_encode(image, use_float=False)
>>> data.shape, data.dtype
((3, 1870, 1606), dtype('uint8'))

Note

The function rgb_encode()’s result is the same as torchvision.transforms.functional import to_tensor’s result when the given image is in RGB mode.