Image printing#

kornia.image.image_to_string(image, max_width=256)[source]#

Obtain the closest xterm-256 approximation string from an image torch.Tensor.

Each pixel is shown with the nearest of the 240 fixed xterm-256 colours (codes 16-255, the 6x6x6 cube and the gray ramp) by RGB distance, measured from its unrounded 0-255 value; codes 0-15 are defined by the terminal and are not used.

Convention:
  • The image is \((C, H, W)\) with 1 or 3 channels. A float image is read in [0, 1] and an integer one in [0, 255], so integer 255 and float 1.0 print alike; a value outside the range raises.

  • Each pixel prints as two spaces on its background colour, one text line per image row. An image wider than max_width pixels is first resized to max_width pixels wide, keeping its aspect ratio, so a line is at most 2 * max_width terminal columns wide.

Parameters:
  • image (Tensor) – an RGB image with shape \(3HW\) or a grayscale image with shape \(1HW\).

  • max_width (int, optional) – the widest image, in pixels, that is printed without resizing. Default: 256

Raises:
  • ImageError – if the tensor is not an image with 1 or 3 channels, such as (H, W) (also a ValueError).

  • ShapeError – if a tensor with 1 or 3 channels is not (C, H, W), such as a batch (also a ValueError).

  • ValueCheckError – if a float tensor has values outside [0, 1], or an integer one outside [0, 255].

Return type:

str

kornia.image.print_image(image, max_width=96)[source]#

Print an image to the terminal.

https://github.com/kornia/data/blob/main/print_image.png?raw=true
Convention:

See the Convention block of image_to_string(), which renders the image; max_width defaults to 96 here and to 256 there. A path is loaded with load_image() as ImageLoadType.RGB8.

Parameters:
  • image (Union[str, PathLike, Tensor]) – path (str or os.PathLike) to a valid image file, or a torch.Tensor.

  • max_width (int, optional) – the widest image, in pixels, that is printed without resizing. Default: 96

Raises:
  • TypeError – if image is neither a path nor a tensor.

  • ImageError – if the tensor is not an image with 1 or 3 channels, such as (H, W) (also a ValueError).

  • ShapeError – if a tensor with 1 or 3 channels is not (C, H, W), such as a batch (also a ValueError).

  • ValueCheckError – if a float tensor has values outside [0, 1], or an integer one outside [0, 255].

  • FileNotFoundError – if the path does not exist. Other errors of kornia.io.load_image() pass through.

Return type:

None