Skip to main content
The Image Codecs (imgcodecs) module handles reading and writing images in various formats, supporting both static images and animations with metadata.

Overview

From opencv2/imgcodecs.hpp:48-55:
This module provides image file reading and writing capabilities with support for various formats including JPEG, PNG, TIFF, WebP, AVIF, GIF, and more. It also handles multi-page images, animations, and metadata (EXIF, XMP, ICC profiles).

imread

Load images from files with automatic format detection

imwrite

Save images to files with format-specific parameters

Animations

Read and write animated GIF, AVIF, APNG, and WebP

Metadata

Handle EXIF, XMP, and ICC profile metadata

Reading Images

Basic Image Reading

From imgcodecs.hpp:332-384:

Image Read Flags

From imgcodecs.hpp:69-85:

Reading with Metadata

From imgcodecs.hpp:397-412:

Supported Formats

From imgcodecs.hpp:340-355, OpenCV supports:

Always Available

  • BMP - Windows Bitmap (*.bmp, *.dib)
  • GIF - Graphics Interchange Format (*.gif)
  • PBM/PGM/PPM/PXM/PNM - Portable image formats
  • Sun Rasters - (*.sr, *.ras)
  • HDR - Radiance HDR (*.hdr, *.pic)

With External Libraries

  • JPEG - (*.jpeg, *.jpg, *.jpe) - requires libjpeg
  • JPEG 2000 - (*.jp2) - requires libjasper/OpenJPEG
  • PNG - (*.png) - requires libpng
  • WebP - (*.webp) - requires libwebp
  • AVIF - (*.avif) - requires libavif
  • TIFF - (*.tiff, *.tif) - requires libtiff
  • OpenEXR - (*.exr) - requires OpenEXR
  • PFM - Portable Float Map (*.pfm)
  • JPEG XL - (*.jxl) - requires libjxl
Format Detection: OpenCV determines the image format by content, not by file extension. The extension is only used when writing images.

Writing Images

Basic Image Writing

From imgcodecs.hpp:511-569:

Write Parameters

From imgcodecs.hpp:88-130, format-specific parameters:

Saving Images with Alpha Channel

From imgcodecs.hpp:533-536:

Multi-Page Images

From imgcodecs.hpp:414-434:

Animation Support

From imgcodecs.hpp:290-330, OpenCV supports animated images:

Reading Animations

Writing Animations

GIF Frame Duration: GIF durations must be multiples of 10ms due to format limitations. Values are automatically rounded down.

Memory Buffer Operations

From imgcodecs.hpp:595-694:

Decoding from Memory

Encoding to Memory

Format Capabilities

Checking Format Support

From imgcodecs.hpp:696-726:

Image Collections

From imgcodecs.hpp:728-758, iterate through multi-page images:

Practical Examples

Convert Image Format

Batch Process Images

Create Thumbnail Grid

Best Practices

Always Check Return Values:
Choose the Right Format:
  • JPEG - Best for photographs, lossy compression
  • PNG - Lossless, supports transparency, larger files
  • WebP/AVIF - Modern formats with better compression
  • TIFF - Multi-page, lossless, supports 16/32-bit
  • EXR - High dynamic range (HDR) images
Memory Considerations: For large images or batch processing:

Color Channel Order

Important: BGR vs RGBOpenCV uses BGR channel order by default (not RGB). When reading images:

Source Reference

Main header: ~/workspace/source/modules/imgcodecs/include/opencv2/imgcodecs.hpp