kornia.image#

Module to provide a high level API to process images.

class kornia.image.ImageSize(height, width)#

Data class to represent image shape.

Parameters:

Example

>>> size = ImageSize(3, 4)
>>> size.height
3
>>> size.width
4
height: int | Tensor#
width: int | Tensor#
class kornia.image.PixelFormat(color_space, bit_depth)#

Data class to represent the pixel format of an image.

Parameters:
  • color_space (ColorSpace) – color space.

  • bit_depth (int) – the number of bits per channel.

Example

>>> pixel_format = PixelFormat(ColorSpace.RGB, 8)
>>> pixel_format.color_space
<ColorSpace.RGB: 2>
>>> pixel_format.bit_depth
8
bit_depth: int#
color_space: ColorSpace#
class kornia.image.ChannelsOrder(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

Enum that represents the channels order of an image.

CHANNELS_FIRST = 0#
CHANNELS_LAST = 1#
class kornia.image.ImageLayout(image_size, channels, channels_order)#

Data class to represent the layout of an image.

Parameters:
  • image_size (ImageSize) – image size.

  • channels (int) – number of channels.

  • channels_order (ChannelsOrder) – channels order.

Example

>>> layout = ImageLayout(ImageSize(3, 4), 3, ChannelsOrder.CHANNELS_LAST)
>>> layout.image_size
ImageSize(height=3, width=4)
>>> layout.channels
3
>>> layout.channels_order
<ChannelsOrder.CHANNELS_LAST: 1>
channels: int#
channels_order: ChannelsOrder#
image_size: ImageSize#
class kornia.image.Image(data, pixel_format, layout)#

Class that holds an Image Tensor representation.

Note

Disclaimer: This class provides the minimum functionality for image manipulation. However, as soon as you start to experiment with advanced tensor manipulation, you might expect fancy polymorphic behaviours.

Warning

This API is experimental and might suffer changes in the future.

Parameters:
  • data (Tensor) – a torch tensor containing the image data.

  • layout (ImageLayout) – a dataclass containing the image layout information.

Examples

>>> # from a torch.tensor
>>> data = torch.randint(0, 255, (3, 4, 5), dtype=torch.uint8)  # CxHxW
>>> pixel_format = PixelFormat(
...     color_space=ColorSpace.RGB,
...     bit_depth=8,
... )
>>> layout = ImageLayout(
...     image_size=ImageSize(4, 5),
...     channels=3,
...     channels_order=ChannelsOrder.CHANNELS_FIRST,
... )
>>> img = Image(data, pixel_format, layout)
>>> assert img.channels == 3
>>> # from a numpy array (like opencv)
>>> data = np.ones((4, 5, 3), dtype=np.uint8)  # HxWxC
>>> img = Image.from_numpy(data, color_space=ColorSpace.RGB)
>>> assert img.channels == 3
>>> assert img.width == 5
>>> assert img.height == 4
property channels: int#

Return the number channels of the image.

property channels_order: ChannelsOrder#

Return the channels order.

clone()#

Return a copy of the image.

Return type:

Image

property data: Tensor#

Return the underlying tensor data.

property device: device#

Return the image device.

property dtype: dtype#

Return the image data type.

float()#

Return the image as float.

Return type:

Image

classmethod from_dlpack(data)#

Construct an image tensor from a DLPack capsule.

Parameters:

data (Any) – a DLPack capsule from numpy, tvm or jax.

Return type:

Image

Example

>>> x = np.ones((4, 5, 3))
>>> img = Image.from_dlpack(x.__dlpack__())
classmethod from_file(file_path)#

Construct an image tensor from a file.

Parameters:

file_path (str | Path) – the path to the file to read the image from.

Return type:

Image

classmethod from_numpy(data, color_space=ColorSpace.RGB, channels_order=ChannelsOrder.CHANNELS_LAST)#

Construct an image tensor from a numpy array.

Parameters:
  • data (Any) – a numpy array containing the image data.

  • color_space (ColorSpace, optional) – the color space of the image. Default: ColorSpace.RGB

  • pixel_format – the pixel format of the image.

Return type:

Image

Example

>>> data = np.ones((4, 5, 3), dtype=np.uint8)  # HxWxC
>>> img = Image.from_numpy(data, color_space=ColorSpace.RGB)
>>> assert img.channels == 3
>>> assert img.width == 5
>>> assert img.height == 4
property height: int#

Return the image height (columns).

property image_size: ImageSize#

Return the image size.

property layout: ImageLayout#

Return the image layout.

property pixel_format: PixelFormat#

Return the pixel format.

print(max_width=256)#

Print the image tensor to the console.

Parameters:

max_width (int, optional) – the maximum width of the image to print. Default: 256

Return type:

None

img = Image.from_file("panda.png")
img.print()
https://github.com/kornia/data/blob/main/print_image.png?raw=true
property shape: tuple[int, ...]#

Return the image shape.

to(device=None, dtype=None)#

Move the image to the given device and dtype.

Parameters:
  • device (Union[str, device, None], optional) – the device to move the image to. Default: None

  • dtype (Optional[dtype], optional) – the data type to cast the image to. Default: None

Returns:

the image moved to the given device and dtype.

Return type:

Image

to_color_space(color_space)#

Convert the image to a different color space.

Return type:

Image

to_dlpack()#

Return a DLPack capsule from the image tensor.

Return type:

Any

to_numpy()#

Return a numpy array in cpu from the image tensor.

Return type:

Any

property width: int#

Return the image width (rows).

write(file_path)#

Write the image to a file.

For now, only support writing to JPEG format.

Parameters:

file_path (str | Path) – the path to the file to write the image to.

Return type:

None

Example

>>> data = np.ones((4, 5, 3), dtype=np.uint8)  # HxWxC
>>> img = Image.from_numpy(data)
>>> img.write("test.jpg")