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> #ifdef ENABLE_VISP_NAMESPACE using namespace VISP_NAMESPACE_NAME; #endif 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/core/vpConfig.h> #include <visp3/io/vpImageIo.h> int main() { #ifdef ENABLE_VISP_NAMESPACE using namespace VISP_NAMESPACE_NAME ; #endif 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
Overloaded function.
Overloaded function.
Overloaded function.
Load an image in portable float map format.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Save an image in portable float map format.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Overloaded function.
Inherited Methods
Operators
__doc__
__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.
- __init__(*args, **kwargs)¶
- static read(*args, **kwargs)¶
Overloaded function.
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).
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.
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.
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.
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 .
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.
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.
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.
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.
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.
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 .
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.
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.
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.
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.
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.
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) .
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 .
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.
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.
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.
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.
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.