Image Utils

This module provides read, write and resize functions for images. The backends of these functions are automatically changed, depending on the user`s environment. The priority of the backends is as below (upper is higher priority):

  • OpenCV (cv2)

  • scikit-image (skimage)

  • pillow (PIL) (need to be installed)

At least one of these modules needs to be installed to use this module.

nnabla.utils.image_utils.imread(path, grayscale=False, size=None, interpolate='bilinear', channel_first=False, as_uint16=False, num_channels=- 1, **kwargs)[source]

Read image from path. If you specify the size, the output array is resized. Default output shape is (height, width, channel) for RGB image and (height, width) for gray-scale image.

Parameters
  • path (String or File Object) – Input image path.

  • grayscale (bool) – If True, the img is rescaled to gray-scale. Default is False.

  • size (tuple of int) – Output shape. The order is (width, height). If None, the image is not resized. Default is None.

  • interpolate (str) –

    Interpolation method. This argument is depend on the backend. If you want to specify this argument, you should pay much attention to which backend you use now. What you can select is below:

    • pil backend: [“nearest”, “box”, “bilinear”, “hamming”, “bicubic”, “lanczos”].

    • cv2 backend: [“nearest”, “bilinear”, “bicubic”, “lanczos”].

    Default is “bilinear” for both backends.

  • channel_first (bool) – If True, the shape of the output array is (channel, height, width) for RGB image. Default is False.

  • as_uint16 (bool) – If True, this function tries to read img as np.uint16. Default is False.

  • num_channels (int) – channel size of output array. Default is -1 which preserves raw image shape.

  • return_palette_indices (bool) – This argument can be used only by pil backend. On pil backend, if this flag is True and PIL.Image has the mode “P”, then this function returns 2-D array containing the indices into palette. Otherwise, 3-D array of “RGB” or “RGBA” (it depends on an image info) will be returned. Default value is False.

Returns

if as_uint16=True output dtype is np.uint16, else np.uint8 (default).

Return type

numpy.ndarray

nnabla.utils.image_utils.imsave(path, img, channel_first=False, as_uint16=False, auto_scale=True, **kwargs)[source]

Save img to the file specified by path. As default, the shape of img has to be (height, width, channel).

Parameters
  • path (str) – Output path.

  • img (numpy.ndarray) – Input image. All pixel values must be positive and in the range [0, 255] of int for uint8, [0, 65535] of int for uint16 or [0, 1] for float. When you pass float image, you must set auto_scale as True (If not, exception will be raised). If img with negative values is passed as input, exception will be raised.

  • channel_first (bool) – If True, you can input the image whose shape is (channel, height, width). Default is False.

  • as_uint16 (bool) – If True, cast image to uint16 before save. Default is False.

  • auto_scale (bool) – Whether the range of pixel values are scaled up or not. The range of upscaled pixel values depends on output dtype, which is [0, 255] as uint8 and [0, 65535] as uint16.

nnabla.utils.image_utils.imresize(img, size, interpolate='bilinear', channel_first=False, **kwargs)[source]

Resize img to size. As default, the shape of input image has to be (height, width, channel).

Parameters
  • img (numpy.ndarray) – Input image.

  • size (tuple of int) – Output shape. The order is (width, height).

  • interpolate (str) –

    Interpolation method. This argument is depend on the backend. If you want to specify this argument, you should pay much attention to which backend you use now. What you can select is below:

    • pil backend: [“nearest”, “box”, “bilinear”, “hamming”, “bicubic”, “lanczos”].

    • cv2 backend: [“nearest”, “bilinear”, “bicubic”, “lanczos”].

    Default is “bilinear” for both backends.

  • channel_first (bool) – If True, the shape of the output array is (channel, height, width) for RGB image. Default is False.

Returns

numpy.ndarray