Python API

This section includes information for using the pure Python API of bob.io.image.

bob.io.image.bob_to_opencvbgr(img)[source]

Returns a view of the image from Bob format to OpenCV BGR format. This function works with images, batches of images, videos, and higher dimensional arrays that contain images.

Parameters

img (numpy.ndarray) – An image loaded by Bob. It needs to have at least 3 dimensions.

Returns

A view of the img compatible with OpenCV.

Return type

numpy.ndarray

Raises

ValueError – If the image dimension is less than 3.

bob.io.image.get_config()[source]

Returns a string containing the configuration information.

bob.io.image.get_correct_image_extension(image_name) extension

Estimates the image type and return a corresponding extension based on file content

This function loads the first bytes of the given image, and matches it with known magic numbers of image files. If a match is found, it returns the corresponding image extension (including the leading '.' that can be used, e.g., in bob.io.image.load().

Parameters:

image_name : str

The name (including path) of the image to check

Returns:

extension : str

The extension of the image based on the file content

bob.io.image.get_include_directories() includes[source]

Returns a list of include directories for dependent libraries, such as libjpeg, libtiff, … This function is automatically used by bob.extension.get_bob_libraries() to retrieve the non-standard include directories that are required to use the C bindings of this library in dependent classes. You shouldn’t normally need to call this function by hand.

Returns:

includes[str]

The list of non-standard include directories required to use the C bindings of this class. For now, only the directory for the HDF5 headers are returned.

bob.io.image.get_macros() macros[source]

Returns a list of preprocessor macros, such as (HAVE_LIBJPEG, 1). This function is automatically used by bob.extension.get_bob_libraries() to retrieve the prerpocessor definitions that are required to use the C bindings of this library in dependent classes. You shouldn’t normally need to call this function by hand.

Returns:

macros[str]

The list of preprocessor macros required to use the C bindings of this class.

bob.io.image.imshow(img, cmap=None, **kwargs)

Plots the images that are returned by bob.io.base.load()

Parameters
  • img (numpy.ndarray) – A 2 or 3 dimensional array containing an image in bob style: For a 2D array (grayscale image) should be (h, w); A 3D array (color image) should be in the (c, h, w) format.

  • cmap (matplotlib.colors.Colormap) – Colormap, optional, default: None. If cmap is None and img.ndim is 2, defaults to ‘gray’. cmap is ignored when img has RGB(A) information.

  • **kwargs – These are passed directly to matplotlib.pyplot.imshow()

Returns

Returns whatever plt.imshow returns.

Return type

object

bob.io.image.load(filename, extension) image[source]

This function loads and image from the file with the specified filename. The type of the image will be determined based on the extension parameter, which can have the following values:

  • None: The file name extension of the filename is used to determine the image type.

  • 'auto': The type of the image will be detected automatically, using bob.io.image.get_correct_image_extension().

  • '.xxx': The image type is determined by the given extension.

    For a list of possible extensions, see bob.io.base.extensions() (only the image extensions are valid here).

Parameters:

filenamestr

The name of the image file to load.

extensionstr

[Default: None] If given, the given extension will determine the type of the image. Use 'auto' to automatically determine the extension (this might take slightly more time).

Returns

image2D or 3D numpy.ndarray of type uint8

The image read from the specified file.

bob.io.image.opencvbgr_to_bob(img)[source]

Returns a view of the image from OpenCV BGR format to Bob RGB format. This function works with images, batches of images, videos, and higher dimensional arrays that contain images.

Parameters

img (numpy.ndarray) – An image loaded by OpenCV. It needs to have at least 3 dimensions.

Returns

A view of the img compatible with Bob.

Return type

numpy.ndarray

Raises

ValueError – If the image dimension is less than 3.

bob.io.image.read(filename, extension=None)

load(filename, extension) -> image

This function loads and image from the file with the specified filename. The type of the image will be determined based on the extension parameter, which can have the following values:

  • None: The file name extension of the filename is used to determine the image type.

  • 'auto': The type of the image will be detected automatically, using bob.io.image.get_correct_image_extension().

  • '.xxx': The image type is determined by the given extension.

    For a list of possible extensions, see bob.io.base.extensions() (only the image extensions are valid here).

Parameters:

filenamestr

The name of the image file to load.

extensionstr

[Default: None] If given, the given extension will determine the type of the image. Use 'auto' to automatically determine the extension (this might take slightly more time).

Returns

image2D or 3D numpy.ndarray of type uint8

The image read from the specified file.

bob.io.image.to_bob(img)[source]

Returns a view of the image from matplotlib format to Bob format. This function works with images, batches of images, videos, and higher dimensional arrays that contain images.

Parameters

img (numpy.ndarray) – An image in matplotlib format (channels last): For an ND array (N >= 3), the image should have the following format: (..., h, w, c).

Returns

A view of the img compatible with Bob.

Return type

numpy.ndarray

bob.io.image.to_matplotlib(img)

Returns a view of the image from Bob format to matplotlib format. This function works with images, batches of images, videos, and higher dimensional arrays that contain images.

Parameters

img (numpy.ndarray) – A N dimensional array containing an image in Bob format (channels first): For an ND array (N >= 3), the image should have the following format: (..., c, h, w).

Returns

A view of the img compatible with matplotlib.pyplot.imshow().

Return type

numpy.ndarray