pyiqa.utils.img_util ==================== .. py:module:: pyiqa.utils.img_util Module Contents --------------- .. py:function:: is_image_file(filename) .. py:function:: scandir_images(dir, max_dataset_size=float('inf'), followlinks=True) Get all image files from a directory and return a sorted list of fullpath. .. py:function:: imread2pil(img_source, rgb=False) Read image to tensor. :param img_source: image filepath string, image contents as a bytearray or a PIL Image instance :type img_source: str, bytes, or PIL.Image :param rgb: convert input to RGB if true .. py:function:: imread2tensor(img_source, rgb=False) Read image to tensor. :param img_source: image filepath string, image contents as a bytearray or a PIL Image instance :type img_source: str, bytes, or PIL.Image :param rgb: convert input to RGB if true .. py:function:: img2tensor(imgs, bgr2rgb=True, float32=True) Numpy array to tensor. :param imgs: Input images. :type imgs: list[ndarray] | ndarray :param bgr2rgb: Whether to change bgr to rgb. :type bgr2rgb: bool :param float32: Whether to change to float32. :type float32: bool :returns: Tensor images. If returned results only have one element, just return tensor. :rtype: list[tensor] | tensor .. py:function:: tensor2img(tensor, rgb2bgr=True, out_type=np.uint8, min_max=(0, 1)) Convert torch Tensors into image numpy arrays. After clamping to [min, max], values will be normalized to [0, 1]. :param 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. :type tensor: Tensor or list[Tensor] :param rgb2bgr: Whether to change rgb to bgr. :type rgb2bgr: bool :param out_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``. :type out_type: numpy type :param min_max: min and max values for clamp. :type min_max: tuple[int] :returns: 3D ndarray of shape (H x W x C) OR 2D ndarray of shape (H x W). The channel order is BGR. :rtype: (Tensor or list) .. py:function:: tensor2img_fast(tensor, rgb2bgr=True, min_max=(0, 1)) This implementation is slightly faster than tensor2img. It now only supports torch tensor with shape (1, c, h, w). :param tensor: Now only support torch tensor with (1, c, h, w). :type tensor: Tensor :param rgb2bgr: Whether to change rgb to bgr. Default: True. :type rgb2bgr: bool :param min_max: min and max values for clamp. :type min_max: tuple[int] .. py:function:: imfrombytes(content, flag='color', float32=False) Read an image from bytes. :param content: Image bytes got from files or other streams. :type content: bytes :param flag: Flags specifying the color type of a loaded image, candidates are `color`, `grayscale` and `unchanged`. :type flag: str :param float32: Whether to change to float32., If True, will also norm to [0, 1]. Default: False. :type float32: bool :returns: Loaded image array. :rtype: ndarray .. py:function:: imwrite(img, file_path, params=None, auto_mkdir=True) Write image to file. :param img: Image array to be written. :type img: ndarray :param file_path: Image file path. :type file_path: str :param params: Same as opencv's :func:`imwrite` interface. :type params: None or list :param auto_mkdir: If the parent folder of `file_path` does not exist, whether to create it automatically. :type auto_mkdir: bool :returns: Successful or not. :rtype: bool .. py:function:: crop_border(imgs, crop_border) Crop borders of images. :param imgs: Images with shape (h, w, c). :type imgs: list[ndarray] | ndarray :param crop_border: Crop border for each end of height and weight. :type crop_border: int :returns: Cropped images. :rtype: list[ndarray]