NvImgCodec

NvImgCodec is NVIDIA’s hardware-accelerated image codec library, providing high-performance decoding and encoding for various image formats (JPEG, PNG, TIFF, etc.). It can decode images directly to GPU memory.

Key Points:

  • Decodes images directly to GPU memory

  • Supports hardware-accelerated encoding

  • Integrates seamlessly with CV-CUDA via CUDA Array Interface

Required Imports:

from pathlib import Path

import cvcuda

from nvidia import nvimgcodec

Setup NvImgCodec:

# setup paths
img_path = (
    Path(__file__).parent.parent / "assets" / "images" / "tabby_tiger_cat.jpg"
)
cvcuda_root = Path(__file__).parent.parent.parent
output_dir = cvcuda_root / ".cache"
output_dir.mkdir(parents=True, exist_ok=True)
output_path = output_dir / "tabby_tiger_cat_224_224.jpg"

# create encoder and decoder
decoder = nvimgcodec.Decoder()
encoder = nvimgcodec.Encoder()

NvImgCodec to CV-CUDA:

nvimgcodec_image = decoder.read(str(img_path))
cvcuda_tensor = cvcuda.as_tensor(nvimgcodec_image, "HWC")

The second parameter "HWC" specifies the layout (Height × Width × Channels). NvImgCodec images are decoded directly to GPU memory and can be immediately converted to CV-CUDA tensors.

Process with CV-CUDA:

resized_cvcuda_tensor = cvcuda.resize(
    cvcuda_tensor, (224, 224, 3), cvcuda.Interp.LINEAR
)

You can apply any CV-CUDA operation to the tensor. Here we resize the image to 224×224 using cubic interpolation.

CV-CUDA to NvImgCodec:

new_nvimgcodec_image = nvimgcodec.as_image(resized_cvcuda_tensor.cuda())
encoder.write(str(output_path), new_nvimgcodec_image)

The processed CV-CUDA tensor can be converted back to an NvImgCodec image and encoded to disk, all without leaving GPU memory.

Typical Use Cases:

  • Batch image preprocessing for inference

  • Image transformation pipelines (resize, color conversion, etc.)

  • High-throughput image processing

  • Building end-to-end GPU pipelines from disk to inference

Complete Example: See samples/interoperability/nvimgcodec_interop.py