Image container#
- class kornia.image.ImageSize(height, width)[source]#
Data class to represent the size of an image, without its channels.
- Convention:
See the Convention block of
Image. The positional order is(height, width), kornia’s order for sizes and the reverse of \((x, y)\) points.
Example
>>> size = ImageSize(3, 4) >>> size.height 3 >>> size.width 4
- class kornia.image.PixelFormat(color_space, bit_depth)[source]#
Data class to represent the pixel format of an image.
- Convention:
See the Convention block of
Image, which checks a pixel format against the data; the data class itself checks nothing.
- Parameters:
color_space (
ColorSpace) – color space.bit_depth (
int) – the storage size of one channel value in bits,8 * element_size()of the data: 8 foruint8, 32 forfloat32.
Example
>>> pixel_format = PixelFormat(ColorSpace.RGB, 8) >>> pixel_format.color_space <ColorSpace.RGB: 2> >>> pixel_format.bit_depth 8
- color_space: ColorSpace#
- class kornia.image.ColorSpace(value)[source]#
Enum that represents the color space of an image.
- Convention:
See the Convention block of
Image, which checks the color space against the channel count. There is no color space with alpha: a 4-channel image isUNKNOWN.
- BGR = 3#
- GRAY = 1#
- RGB = 2#
- UNKNOWN = 0#
- class kornia.image.ChannelsOrder(value)[source]#
Enum that represents the channels order of an image.
- Convention:
See the Convention block of
Image.CHANNELS_FIRSTstores \((C, H, W)\) andCHANNELS_LASTstores \((H, W, C)\).
- CHANNELS_FIRST = 0#
- CHANNELS_LAST = 1#
- class kornia.image.ImageLayout(image_size, channels, channels_order)[source]#
Data class to represent the layout of an image.
- Convention:
See the Convention block of
Image, which checks a layout against the data; the data class itself checks nothing.
- 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_order: ChannelsOrder#
- class kornia.image.Image(data, pixel_format, layout)[source]#
Class that holds an Image torch.Tensor representation.
Warning
This API is experimental and might suffer changes in the future.
- Convention:
An
Imageholds one image, \((C, H, W)\) or \((H, W, C)\) aslayout.channels_ordersays, never a batch: a 4-D tensor raisesShapeError. The layout and the pixel format are checked against the data:bit_depthis the storage size,8 * data.element_size(), and the color space must fit the channel count, 1 forGRAY, 3 forRGBandBGR, any forUNKNOWN.from_numpy(),to_numpy(),from_dlpack()andto_dlpack()share memory with the array or capsule of a CPU image.to_numpy()returns the stored layout, so an image fromfrom_file()gives \((C, H, W)\).to_gray(),to_rgb()andto_bgr()return the image itself when it is already in that color space, an image with new data when it is in another of the three, and raiseValueErrorforUNKNOWN. Converting RGB or BGR to gray requiresuint8or a floating dtype supported byrgb_to_grayscale(); other integer dtypes, such asuint16, are unsupported even thoughImagecan hold them. An already-gray image is returned unchanged regardless of dtype.to(),float()andclone()return a newImageand leave this one unchanged.
- Parameters:
data (
Tensor) – a torch.Tensor containing the image data.pixel_format (
PixelFormat) – the pixel format of the image.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_order: ChannelsOrder#
Return the channels order.
- classmethod from_dlpack(data)[source]#
Construct an
Imagefrom a DLPack capsule, sharing its memory.- Parameters:
data (
Any) – a DLPack capsule from numpy, tvm or jax. The capsule must hold a(C, H, W)image; it is labelledGRAYfor 1 channel,RGBfor 3 andUNKNOWNotherwise.- Raises:
ShapeError – if the capsule has more than three dimensions. It is also a
ValueError.IndexError – if the capsule has fewer than three dimensions.
- Return type:
Example
>>> x = np.ones((3, 4, 5)) # (C, H, W) >>> img = Image.from_dlpack(x.__dlpack__()) >>> img.channels, img.height, img.width (3, 4, 5)
- classmethod from_file(file_path)[source]#
Construct an
Imagefrom a file.The file is loaded with
load_image()asImageLoadType.RGB8: the image is \((3, H, W)\)uint8,RGBandCHANNELS_FIRST, on the CPU.
- classmethod from_numpy(data, color_space=None, channels_order=ChannelsOrder.CHANNELS_LAST)[source]#
Construct an
Imagefrom a numpy array, sharing its memory.- Parameters:
data (
Any) – a numpy array containing the image data.color_space (
Optional[ColorSpace], optional) – the color space of the image. IfNone, a 1-channel array isGRAY, a 3-channel array isRGBand any other channel count isUNKNOWN. An explicit color space that does not match the channel count raises. Default:Nonechannels_order (
ChannelsOrder, optional) – what dimension the channels are in the image torch.Tensor. Default:ChannelsOrder.CHANNELS_LAST
- Raises:
ValueError – if
channels_orderis neitherCHANNELS_LASTnorCHANNELS_FIRST.ValueCheckError – if an explicit
color_spacedoes not match the channel count (also aValueError).ShapeError – if
datahas more than three dimensions. It is also aValueError.IndexError – if
datahas fewer than two dimensions.
- Return type:
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 layout: ImageLayout#
Return the image layout.
- property pixel_format: PixelFormat#
Return the pixel format.
- print(max_width=256)[source]#
Print the image torch.Tensor to the console.
- Parameters:
max_width (
int, optional) – the widest image, in pixels, that is printed without resizing; seeimage_to_string(). Default:256- Return type:
A
BGRimage is converted to RGB first, so the terminal shows the true colours.img = Image.from_file("panda.png") img.print()
- to(device=None, dtype=None)[source]#
Return the image on
deviceand withdtype, liketorch.Tensor.to().The image itself is not changed; assign the result (
img = img.to(...)). The pixel format’sbit_depthfollows the new dtype.- Parameters:
- Returns:
the image on the given device and with the given dtype.
- Return type:
- Raises:
ValueCheckError – if the image was built with checks disabled and its color space does not fit its channels.
- to_numpy()[source]#
Return the image data as a numpy array on the CPU, in the stored layout.
- Return type:
- write(file_path)[source]#
Write the image to a file.
The format follows the file extension; see
kornia.io.write_image()for the supported formats. ABGRimage is converted to RGB first, so the file holds the true colours.Example
>>> data = np.ones((4, 5, 3), dtype=np.uint8) # HxWxC >>> img = Image.from_numpy(data) >>> import tempfile, os >>> with tempfile.TemporaryDirectory() as tmp: ... img.write(os.path.join(tmp, "test.jpg"))