ImageIo

class ImageIo

Bases: pybind11_object

Read/write images with various image format.

This class has its own implementation of PGM and PPM images read/write.

This class may benefit from optional 3rd parties:

  • libpng: If installed this optional 3rd party is used to read/write PNG images. Installation instructions are provided here https://visp.inria.fr/3rd_png .

  • libjpeg: If installed this optional 3rd party is used to read/write JPEG images. Installation instructions are provided here https://visp.inria.fr/3rd_jpeg .

  • OpenCV: If installed this optional 3rd party is used to read/write other image formats TIFF, BMP, DIB, PBM, RASTER, JPEG2000. If libpng or libjpeg is not installed OpenCV is also used to consider these image formats. Installation instructions are provided here https://visp.inria.fr/3rd_opencv .

The code below shows how to convert an PPM P6 image file format into a PGM P5 image file format. The extension of the filename is here used in read() and write() functions to set the image file format (“.pgm” for PGM P5 and “.ppm” for PPM P6).

#include <visp3/io/vpImageIo.h>

int main()
{
  vpImage<unsigned char> I;
#if defined(_WIN32)
  std::string filename("C:/Temp/visp-images/Klimt/Klimt.ppm");
#else // UNIX
  std::string filename("/local/soft/ViSP/ViSP-images/Klimt/Klimt.ppm");
#endif

  vpImageIo::read(I, filename); // Convert the color image in a gray level image
  vpImageIo::write(I, "Klimt.pgm"); // Write the image in a PGM P5 image file format
}

This other example available in tutorial-image-reader.cpp shows how to read/write jpeg images. It supposes that libjpeg is installed.

#include <visp3/io/vpImageIo.h>

int main()
{
  try {
    vpImage<vpRGBa>  I;
    vpImageIo::read (I, "monkey.jpeg");
    vpImageIo::write (I, "monkey.png");
  } catch (const vpException  &e) {
    std::cout << e. getMessage () << std::endl;
  } catch (...) {
    std::cout << "Unsupported image format" << std::endl;
  }
}

Methods

__init__

read

Overloaded function.

readEXR

Overloaded function.

readJPEG

Overloaded function.

readPFM

Load an image in portable float map format.

readPFM_HDR

Overloaded function.

readPGM

Overloaded function.

readPNG

Overloaded function.

readPNGfromMem

Overloaded function.

readPPM

Overloaded function.

write

Overloaded function.

writeEXR

Overloaded function.

writeJPEG

Overloaded function.

writePFM

Save an image in portable float map format.

writePFM_HDR

Overloaded function.

writePGM

Overloaded function.

writePNG

Overloaded function.

writePNGtoMem

Overloaded function.

writePPM

Overloaded function.

Inherited Methods

Operators

__doc__

__init__

__module__

Attributes

IO_DEFAULT_BACKEND

IO_OPENCV_BACKEND

IO_SIMDLIB_BACKEND

IO_STB_IMAGE_BACKEND

IO_SYSTEM_LIB_BACKEND

__annotations__

class ImageIoBackendType(self, value: int)

Bases: pybind11_object

Image IO backend for only jpeg and png formats image loading and saving.

Values:

  • IO_DEFAULT_BACKEND: Default backend.

  • IO_SYSTEM_LIB_BACKEND: Use system libraries like libpng or libjpeg-turbo.

  • IO_OPENCV_BACKEND: Use OpenCV imgcodecs module.

  • IO_SIMDLIB_BACKEND: Use embedded simd library.

  • IO_STB_IMAGE_BACKEND: Use embedded stb_image library.

__and__(self, other: object) object
__eq__(self, other: object) bool
__ge__(self, other: object) bool
__getstate__(self) int
__gt__(self, other: object) bool
__hash__(self) int
__index__(self) int
__init__(self, value: int)
__int__(self) int
__invert__(self) object
__le__(self, other: object) bool
__lt__(self, other: object) bool
__ne__(self, other: object) bool
__or__(self, other: object) object
__rand__(self, other: object) object
__ror__(self, other: object) object
__rxor__(self, other: object) object
__setstate__(self, state: int) None
__xor__(self, other: object) object
property name : str
__init__(*args, **kwargs)
static read(*args, **kwargs)

Overloaded function.

  1. read(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Read the contents of the image filename, allocate memory for the corresponding grayscale image, update its content, and return a reference to the image.

If the image has been already initialized, memory allocation is done only if the new image size is different, else we re-use the same memory space.

Supported formats are:

  • portable gray map: *.pgm file

  • portable pix map: *.ppm file

  • portable float map: *.pfm file

  • jpeg: *.jpg , *.jpeg files

  • png: *.png file

If ViSP is build with OpenCV support, additional formats are considered:

  • *.jp2 , *.rs , *.ras , *.tiff , *.tif , *.png , *.bmp , *.pbm files.

If EXIF information is embedded in the image file, the EXIF orientation is ignored.

Parameters:
I

Image to set with the filename content.

filename

Name of the file containing the image.

backend

Library backend type (see vpImageIo::vpImageIoBackendType ) for image reading. This parameter is only used when the image need to be loaded in jpeg or png format. To know which is the default backend see respectively void vpImageIo::readJPEG(const vpImage<unsigned char> &, const std::string &, int) and void vpImageIo::readPNG(const vpImage<unsigned char> &, const std::string &, int).

  1. read(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Read the contents of the image filename, allocate memory for the corresponding grayscale image, update its content, and return a reference to the image.

If the image has been already initialized, memory allocation is done only if the new image size is different, else we re-use the same memory space.

Supported formats are:

  • portable gray map: *.pgm file

  • portable pix map: *.ppm file

  • portable float map: *.pfm file

  • jpeg: *.jpg , *.jpeg files

  • png: *.png file

If ViSP is build with OpenCV support, additional formats are considered:

  • *.jp2 , *.rs , *.ras , *.tiff , *.tif , *.png , *.bmp , *.pbm files.

If EXIF information is embedded in the image file, the EXIF orientation is ignored.

Parameters:
I

Image to set with the filename content.

filename

Name of the file containing the image.

backend

Library backend type (see vpImageIo::vpImageIoBackendType ) for image reading. This parameter is only used when the image need to be loaded in jpeg or png format. To know which is the default backend see respectively void vpImageIo::readJPEG(const vpImage<unsigned char> &, const std::string &, int) and void vpImageIo::readPNG(const vpImage<unsigned char> &, const std::string &, int).

static readEXR(*args, **kwargs)

Overloaded function.

  1. readEXR(I: visp._visp.core.ImageFloat, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load an image in EXR format.

Parameters:
I

Floating-point single channel image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Only OpenCV and the Tiny OpenEXR image libraries can currently read EXR image. The default backend vpImageIo::IO_DEFAULT_BACKEND is the Tiny OpenEXR image library.

  1. readEXR(I: visp._visp.core.ImageRGBf, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load an image in EXR format.

Parameters:
I

Floating-point three channels image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Only OpenCV and the Tiny OpenEXR image libraries can currently read EXR image. The default backend vpImageIo::IO_DEFAULT_BACKEND is the Tiny OpenEXR image library.

static readJPEG(*args, **kwargs)

Overloaded function.

  1. readJPEG(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load a jpeg image. If it is a color image it is converted in gray.

Parameters:
I

Gray level image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_STB_IMAGE_BACKEND .

  1. readJPEG(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load a jpeg image. If it is a gray image it is converted in color.

Parameters:
I

Color image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_STB_IMAGE_BACKEND .

static readPFM(I: visp._visp.core.ImageFloat, filename: str) None

Load an image in portable float map format.

Parameters:
I: visp._visp.core.ImageFloat

Image read from filename.

filename: str

Image location.

static readPFM_HDR(*args, **kwargs)

Overloaded function.

  1. readPFM_HDR(I: visp._visp.core.ImageFloat, filename: str) -> None

Load an image in portable float map format and not restricted to the [0, 255] dynamic range.

Parameters:
I

Image read from filename.

filename

Image location.

  1. readPFM_HDR(I: visp._visp.core.ImageRGBf, filename: str) -> None

Load an image in portable float map format and not restricted to the [0, 255] dynamic range.

Parameters:
I

Image read from filename and with three channels.

filename

Image location.

static readPGM(*args, **kwargs)

Overloaded function.

  1. readPGM(I: visp._visp.core.ImageGray, filename: str) -> None

Load an image in portable gray map format. If the image is in color, it is converted in gray level.

Parameters:
I

Image read from filename.

filename

Image location.

  1. readPGM(I: visp._visp.core.ImageRGBa, filename: str) -> None

Load an image in portable float map format. If the image is in gray, it is converted in color.

Parameters:
I

Image read from filename.

filename

Image location.

static readPNG(*args, **kwargs)

Overloaded function.

  1. readPNG(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load an image in png format. If it is a color image it is converted in gray.

Parameters:
I

Gray level image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_STB_IMAGE_BACKEND .

  1. readPNG(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Load an image in png format. If it is a gray level image it is converted in color.

Parameters:
I

Color image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_STB_IMAGE_BACKEND .

static readPNGfromMem(*args, **kwargs)

Overloaded function.

  1. readPNGfromMem(buffer: list[int], I: visp._visp.core.ImageGray, backend: int = IO_DEFAULT_BACKEND) -> None

Read the content of the grayscale image bitmap stored in memory and encoded using the PNG format.

Parameters:
buffer

Grayscale image buffer encoded in PNG as 1-D unsigned char vector.

I

Output decoded grayscale image.

backend

Supported backends are IO_OPENCV_BACKEND and IO_STB_IMAGE_BACKEND, default IO_DEFAULT_BACKEND will choose IO_OPENCV_BACKEND if available or IO_STB_IMAGE_BACKEND otherwise.

  1. readPNGfromMem(buffer: list[int], I: visp._visp.core.ImageRGBa, backend: int = IO_DEFAULT_BACKEND) -> None

Read the content of the grayscale image bitmap stored in memory and encoded using the PNG format.

Parameters:
buffer

Color image buffer encoded in PNG as 1-D unsigned char vector.

I

Output decoded color image.

backend

Supported backends are IO_OPENCV_BACKEND and IO_STB_IMAGE_BACKEND, default IO_DEFAULT_BACKEND will choose IO_OPENCV_BACKEND if available or IO_STB_IMAGE_BACKEND otherwise.

static readPPM(*args, **kwargs)

Overloaded function.

  1. readPPM(I: visp._visp.core.ImageGray, filename: str) -> None

Load an image in portable pixmap format. If the image is in color, it is converted in gray level.

Parameters:
I

Image read from filename.

filename

Image location.

  1. readPPM(I: visp._visp.core.ImageRGBa, filename: str) -> None

Load an image in portable pixmap format. If the image is in gray, it is converted in color.

Parameters:
I

Image read from filename.

filename

Image location.

static write(*args, **kwargs)

Overloaded function.

  1. write(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Write the content of the image in the file which name is given by filename .

Supported formats are:

  • portable gray map: *.pgm file

  • portable pix map: *.ppm file

  • portable float map: *.pfm file

  • jpeg: *.jpg , *.jpeg files

  • png: *.png file

If ViSP is build with OpenCV support, additional formats are considered:

  • *.jp2 , *.rs , *.ras , *.tiff , *.tif , *.png , *.bmp , *.pbm files.

Parameters:
I

Image to write.

filename

Name of the file containing the image.

backend

Library backend type (see vpImageIo::vpImageIoBackendType ) for image writing. This parameter is only used when the image need to be saved in jpeg or png format. To know which is the default backend see respectively void vpImageIo::writeJPEG(const vpImage<unsigned char> &, const std::string &, int, int) and void vpImageIo::writePNG(const vpImage<unsigned char> &, const std::string &, int) .

  1. write(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Write the content of the image in the file which name is given by filename .

Supported formats are:

  • portable gray map: *.pgm file

  • portable pix map: *.ppm file

  • portable float map: *.pfm file

  • jpeg: *.jpg , *.jpeg files

  • png: *.png file

If ViSP is build with OpenCV support, additional formats are considered:

  • *.jp2 , *.rs , *.ras , *.tiff , *.tif , *.png , *.bmp , *.pbm files.

Parameters:
I

Image to write.

filename

Name of the file containing the image.

backend

Library backend type (see vpImageIo::vpImageIoBackendType ) for image writing. This parameter is only used when the image need to be saved in jpeg or png format. To know which is the default backend see respectively void vpImageIo::writeJPEG(const vpImage<vpRGBa> &, const std::string &, int, int) and void vpImageIo::writePNG(const vpImage<vpRGBa> &, const std::string &, int) .

static writeEXR(*args, **kwargs)

Overloaded function.

  1. writeEXR(I: visp._visp.core.ImageFloat, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Save an image in EXR format.

Parameters:
I

Floating-point single channel image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Only OpenCV and the Tiny OpenEXR image libraries can currently save EXR image. The default backend vpImageIo::IO_DEFAULT_BACKEND is the Tiny OpenEXR image library.

  1. writeEXR(I: visp._visp.core.ImageRGBf, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Save an image in EXR format.

Parameters:
I

Floating-point three channels image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Only OpenCV and the Tiny OpenEXR image libraries can currently save EXR image. The default backend vpImageIo::IO_DEFAULT_BACKEND is the Tiny OpenEXR image library.

static writeJPEG(*args, **kwargs)

Overloaded function.

  1. writeJPEG(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND, quality: int = 90) -> None

Save an image in jpeg format.

Parameters:
I

Gray level image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SIMDLIB_BACKEND .

quality

Image quality percentage in range 0-100.

  1. writeJPEG(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND, quality: int = 90) -> None

Save an image in jpeg format.

Parameters:
I

Color image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SIMDLIB_BACKEND .

quality

Image quality percentage in range 0-100.

static writePFM(I: visp._visp.core.ImageFloat, filename: str) None

Save an image in portable float map format.

Parameters:
I: visp._visp.core.ImageFloat

Image to save.

filename: str

Image location.

static writePFM_HDR(*args, **kwargs)

Overloaded function.

  1. writePFM_HDR(I: visp._visp.core.ImageFloat, filename: str) -> None

Save a high-dynamic range (not restricted to the [0-255] intensity range) floating-point image in portable float map format.

Parameters:
I

Grayscale floating-point image to save.

filename

Image location.

  1. writePFM_HDR(I: visp._visp.core.ImageRGBf, filename: str) -> None

Save a RGB high-dynamic range (not restricted to the [0-255] intensity range) floating-point image in portable float map format.

Parameters:
I

RGB floating-point image to save.

filename

Image location.

static writePGM(*args, **kwargs)

Overloaded function.

  1. writePGM(I: visp._visp.core.ImageGray, filename: str) -> None

Save an image in portable gray map format.

Parameters:
I

Image to save.

filename

Image location.

  1. writePGM(I: vpImage<short>, filename: str) -> None

Save a gray level image in portable gray map format.

Parameters:
I

Image to save.

filename

Image location.

  1. writePGM(I: visp._visp.core.ImageRGBa, filename: str) -> None

Save a color image in portable gray map format.

Parameters:
I

Image to save.

filename

Image location.

static writePNG(*args, **kwargs)

Overloaded function.

  1. writePNG(I: visp._visp.core.ImageGray, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Save an image in png format.

Parameters:
I

Gray level image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SIMDLIB_BACKEND .

  1. writePNG(I: visp._visp.core.ImageRGBa, filename: str, backend: int = IO_DEFAULT_BACKEND) -> None

Save an image in png format.

Parameters:
I

Color image.

filename

Image location.

backend

Supported backends are described in vpImageIo::vpImageIoBackendType . Depending on its availability, the default backend vpImageIo::IO_DEFAULT_BACKEND is chosen in the following order: vpImageIo::IO_OPENCV_BACKEND , vpImageIo::IO_SYSTEM_LIB_BACKEND , vpImageIo::IO_SIMDLIB_BACKEND .

static writePNGtoMem(*args, **kwargs)

Overloaded function.

  1. writePNGtoMem(I: visp._visp.core.ImageGray, buffer: list[int], backend: int = IO_DEFAULT_BACKEND) -> list[int]

In-memory PNG encoding of the grayscale image.

Parameters:
I

Input grayscale image.

buffer

Encoded image as 1-D unsigned char vector using the PNG format.

backend

Supported backends are IO_OPENCV_BACKEND and IO_STB_IMAGE_BACKEND, default IO_DEFAULT_BACKEND will choose IO_OPENCV_BACKEND if available or IO_STB_IMAGE_BACKEND otherwise.

Returns:

A tuple containing:

  • buffer: Encoded image as 1-D unsigned char vector using the PNG format.

  1. writePNGtoMem(I: visp._visp.core.ImageRGBa, buffer: list[int], backend: int = IO_DEFAULT_BACKEND, saveAlpha: bool = false) -> list[int]

In-memory PNG encoding of the color image.

Parameters:
I

Input color image.

buffer

Encoded image as 1-D unsigned char vector using the PNG format.

backend

Supported backends are IO_OPENCV_BACKEND and IO_STB_IMAGE_BACKEND, default IO_DEFAULT_BACKEND will choose IO_OPENCV_BACKEND if available or IO_STB_IMAGE_BACKEND otherwise.

saveAlpha

If true, alpha channel is also used for encoding.

Returns:

A tuple containing:

  • buffer: Encoded image as 1-D unsigned char vector using the PNG format.

static writePPM(*args, **kwargs)

Overloaded function.

  1. writePPM(I: visp._visp.core.ImageGray, filename: str) -> None

Save a gray level image in portable pixmap format.

Parameters:
I

Image to save.

filename

Image location.

  1. writePPM(I: visp._visp.core.ImageRGBa, filename: str) -> None

Save a color level image in portable pixmap format.

Parameters:
I

Image to save.

filename

Image location.