#30087·opencv

WebP lossless encoding of images with an alpha channel is not exact (RGB under alpha=0 is altered)

Author: jantonguiraoCreated Sep 28, 2026Updated Sep 28, 2026

Expected behaviour

Encoding an image to WebP with cv::IMWRITE_WEBP_QUALITY set above 100 (lossless) and then decoding it back should reproduce the original pixel data exactly, the same way it does for 3-channel (RGB) images.

Actual behaviour

For 4-channel (RGBA) images, pixels where alpha == 0 do not round-trip exactly — the RGB values at those pixels are altered by the encode/decode cycle. Alpha itself round-trips fine; only RGB under fully-transparent pixels is affected. 3-channel images and PNG encoding of the same RGBA data round-trip losslessly, so this is specific to the WebP lossless path.

Reproduction

python
import cv2
import numpy as np

def test(num_channels=3):
    print(f"testing with {num_channels} channels")
    image = np.random.randint(0, 255, (100, 100, num_channels), dtype=np.uint8)

    status, encoded = cv2.imencode(".webp", image, [cv2.IMWRITE_WEBP_QUALITY, 101])
    print("encode status:", status)

    decoded = cv2.imdecode(encoded, cv2.IMREAD_UNCHANGED)
    print("are images equal:", (decoded == image).all())

test(3)
test(4)

Output:

testing with 3 channels
encode status: True
are images equal: True

testing with 4 channels
encode status: True
are images equal: False

Further isolation:

  • Channel 3 (alpha) has zero diff — only RGB channels differ.
  • Every mismatched pixel has alpha == 0, and every alpha == 0 pixel mismatches (100% correlation, verified across multiple random seeds/sizes).
  • Encoding the same RGBA data as PNG round-trips exactly, confirming this is WebP-specific.

Root cause

libwebp's lossless encoder has a WebPConfig::exact flag (default off, documented here) that, when disabled, is explicitly permitted to discard/rewrite RGB under fully-transparent pixels to improve compression. Setting exact = 1 preserves them exactly. This is intentional, documented libwebp behaviour — not a libwebp bug.

The problem is that OpenCV's still-image WebP writer never gets a chance to set exact, because it doesn't use the advanced WebPConfig/WebPEncode API at all — WebPEncoder::write() calls the simple one-shot functions WebPEncodeLosslessBGR/WebPEncodeLosslessBGRA (modules/imgcodecs/src/grfmt_webp.cpp), and that simple API has no exact parameter. (The separate writeanimation() path does build a WebPConfig via WebPConfigInit, but even there exact is never set.) Also, IMWRITE_WEBP_QUALITY is the only encode flag WebPEncoder accepts (m_supported_encode_key = {IMWRITE_WEBP_QUALITY}), so there's currently no way for a caller to request exact/lossless-with-alpha behaviour through OpenCV's public API at all.

Environment

  • OpenCV version: 4.11.0 (opencv-python), also reproduced against OpenCV 4.13.0 source
  • Platform: Linux

Suggested fix

Switch WebPEncoder::write() to the advanced WebPConfig/WebPEncode API (as writeanimation() already does) and set config.exact = 1 when encoding a lossless image with an alpha channel, so lossless RGBA output is actually lossless. Optionally expose this as a new cv::ImwriteFlags value if callers should be able to opt in/out explicitly.