Image printing#

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

Render one image tensor as an xterm-256 ANSI text string, without printing it.

text = image_to_string(image) takes a (3, H, W) RGB or (1, H, W) gray image and returns the text that shows it in a terminal. Each pixel is two spaces on the nearest xterm-256 background color, one line per image row. An image wider than max_width pixels is first resized to that width.

Convention:
  • 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.

  • The color of a pixel is the nearest of the 240 fixed xterm-256 colors (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.

  • The resize keeps the aspect ratio, so a line is at most 2 * max_width terminal columns wide.

Parameters:
  • image (Tensor) – an RGB image with shape (3, H, W) or a grayscale image with shape (1, H, W).

  • 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

See also

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

Print an image tensor or an image file to the terminal in xterm-256 colors.

print_image(image) prints image_to_string() of a (3, H, W) or (1, H, W) tensor, or of a file loaded with load_image() as ImageLoadType.RGB8.

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.

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

See also