Image Utils

read_img(fpath, normalize_func=None, yield_path=False)[source]

Read image from supplied path into tensorflow.image tensor.

Parameters
  • fpath – File path to image file.

  • normalize_func

    Normalization function to apply, None means no normalization takes place.
    Functions to either normalize to [0, 1] or [-1, 1] are in simple_sr.utils.image.image_transforms.

  • yield_path – Whether image file path should be returned as well.

Returns

tensorflow.image tensor and optionally the path of the image as well.

tensor_to_img(tensor)[source]

Converts a tensor to PIL.Image object.

Parameters

tensor – Tensor to be converted.

Returns

PIL.Image from input tensor.

reconstruct_from_overlapping_patches(patches, image_height, image_width, pixel_overlap, horizontal_padding, vertical_padding)[source]

Reconstructs an image from supplied overlapping patches.

Parameters
  • patches – Tensor of rank 4 containing the patches that will be stitched together.

  • image_height – Height of the resulting image.

  • image_width – Width of the resulting image.

  • pixel_overlap – Pixel overlap per patch per direction (north, east, south, west) in number of pixels.

  • horizontal_padding – Number of pixel rows that were appended to the bottom of the image before extraction of patches.

  • vertical_padding – Number of pixel columns that were appended on the right side of the image before extraction of patches.

Returns

A Tensor of rank 3 contatining the reconstructed image.

reconstruct_from_patches(patches, original_height, original_width, horizontal_padding=0, vertical_padding=0)[source]

Reconstructs single image from supplied non-overlapping patches.

Parameters
  • patches – Tensor of rank 4 containing patches.

  • original_height – Height of image to be reconstructed.

  • original_width – Width of image to be reconstructed.

  • vertical_padding – Number of columns that were appended to the image before extraction of patches.

  • horizontal_padding – Number of rows that were appended to the image before extraction of patches.

Returns

Tensor of rank 3 containing the combined patches.

segment_into_patches(tensor, patch_width=32, patch_height=32, pixel_overlap=0)[source]

Segments input tensor into patches for memory efficient inference on large pictures.

Parameters
  • tensor – Tensor to segment into patches, needs to be rank 3.

  • patch_width – Width of extracted patches.

  • patch_height – Height of extracted patches.

  • pixel_overlap – Amount of overlap per patch per direction (north, east, south, west) in pixels

Returns

A tuple containing tensor of rank 4 with patches of input tensor, and a tensor of shape (2, 2) containing the padding that was used/needed. Structure of padding: [[number of padded rows top, number of padded rows bottom], [number of padded columns left, number of padded columns right]]

save_single(tensor, save_dir, fname, label=None)[source]

Converts tensors of rank 3 or rank 4 to PIL image objects and saves them to disk.

Parameters
  • tensor – Tensor containing the images to be saved.

  • save_dir – Folder to save images to.

  • fname – Name of files to save images in (will be suffixed with position in tensor if multiple images are saved)

  • label – Optional labels, will be shown in the bottom left of the saved image.

combine_halfs(left_tensor, right_tensor, left_label, save_dir, fname, right_label='interpolated', grid=False)[source]

Creates a combined image from two images by stitching together left and right half respectively of each supplied image. A typical use case would be comparing an image upscaled with Super-Resolution to an image upscaled with interpolation.

Parameters
  • left_tensor

    Tensor containing elements that will be on the left side of the resulting image.
    Tensor may be of rank 3 or rank 4.

  • right_tensor

    Tensor containing elements that will be on the right side of the resulting image.
    Tensor may be of rank 3 or rank 4, must have same dimensions as left_tensor.

  • left_label – String to labels left side of resulting image.

  • right_label – String to labels right side of resulting image.

  • save_dir – Path to save resulting image to.

  • fname – File name to save resulting image under.

  • grid

    Option to additionally plot all stitched images together in a grid.
    left_tensor and right_tensor must be of rank 4 to use this option.

prepare_image_grid(save_dir, fname, low_res_key=None, original=None, psnr=None, ssim=None, **kwargs)[source]

Prepares and saves an image grid for comparison. Supplied images must have the same dimensions.

Parameters
  • save_dir – Path to save image grid to.

  • fname – File name to save image grid in.

  • original – Optionally an original image can be plotted next to the image grid (for instance if image in grid are patches from some image).

  • psnr – Optional dictionary with keys corresponding to keys in kwargs and values containing tensors of PSNR values. PSNR values will be annotated in corresponding pictures.

  • ssim – Optional dictionary with keys corresponding to keys in kwargs and values containing tensors of SSIM values. SSIM values will be annotated in corresponding pictures.

  • low_res_key – Optional key for low-resolution images in kwargs. Images corresponding to this key, will be padded and centered to align with larges images in grid.

  • kwargs – A dictionary containing image tensors as values. Keys will be used as labels strings inside plotted images.