basicsr.utils.img_util

basicsr.utils.img_util.crop_border(imgs, crop_border)[source]

Crop borders of images.

Parameters:
  • imgs (list[ndarray] | ndarray) – Images with shape (h, w, c).

  • crop_border (int) – Crop border for each end of height and weight.

Returns:

Cropped images.

Return type:

list[ndarray]

basicsr.utils.img_util.imfrombytes(content, flag='color', float32=False)[source]

Read an image from bytes.

Parameters:
  • content (bytes) – Image bytes got from files or other streams.

  • flag (str) – Flags specifying the color type of a loaded image, candidates are color, grayscale and unchanged.

  • float32 (bool) – Whether to change to float32., If True, will also norm to [0, 1]. Default: False.

Returns:

Loaded image array.

Return type:

ndarray

basicsr.utils.img_util.img2tensor(imgs, bgr2rgb=True, float32=True)[source]

Numpy array to tensor.

Parameters:
  • imgs (list[ndarray] | ndarray) – Input images.

  • bgr2rgb (bool) – Whether to change bgr to rgb.

  • float32 (bool) – Whether to change to float32.

Returns:

Tensor images. If returned results only have

one element, just return tensor.

Return type:

list[tensor] | tensor

basicsr.utils.img_util.imwrite(img, file_path, params=None, auto_mkdir=True)[source]

Write image to file.

Parameters:
  • img (ndarray) – Image array to be written.

  • file_path (str) – Image file path.

  • params (None or list) – Same as opencv’s imwrite() interface.

  • auto_mkdir (bool) – If the parent folder of file_path does not exist, whether to create it automatically.

Returns:

Successful or not.

Return type:

bool

basicsr.utils.img_util.tensor2img(tensor, rgb2bgr=True, out_type=<class 'numpy.uint8'>, min_max=(0, 1))[source]

Convert torch Tensors into image numpy arrays.

After clamping to [min, max], values will be normalized to [0, 1].

Parameters:
  • tensor (Tensor or list[Tensor]) – Accept shapes: 1) 4D mini-batch Tensor of shape (B x 3/1 x H x W); 2) 3D Tensor of shape (3/1 x H x W); 3) 2D Tensor of shape (H x W). Tensor channel should be in RGB order.

  • rgb2bgr (bool) – Whether to change rgb to bgr.

  • out_type (numpy type) – output types. If np.uint8, transform outputs to uint8 type with range [0, 255]; otherwise, float type with range [0, 1]. Default: np.uint8.

  • min_max (tuple[int]) – min and max values for clamp.

Returns:

3D ndarray of shape (H x W x C) OR 2D ndarray of shape (H x W). The channel order is BGR.

Return type:

(Tensor or list)

basicsr.utils.img_util.tensor2img_fast(tensor, rgb2bgr=True, min_max=(0, 1))[source]

This implementation is slightly faster than tensor2img. It now only supports torch tensor with shape (1, c, h, w).

Parameters:
  • tensor (Tensor) – Now only support torch tensor with (1, c, h, w).

  • rgb2bgr (bool) – Whether to change rgb to bgr. Default: True.

  • min_max (tuple[int]) – min and max values for clamp.