Does 4.8.0 not support AVIF imencode/decode?

import cv2
import socket
import numpy as np


Open the webcam

cap = cv2.VideoCapture(0) # Change the index if you have multiple cameras

while True:
# Capture a frame from the webcam
ret, frame =

# Encode the frame as jpeg, tiff, webp image
# Set the quality (optional)
encode_param_jpeg = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
result_jpeg, encimg_jpeg = cv2.imencode('.jpg', frame, encode_param_jpeg)

encode_param_tiff = [int(cv2.IMWRITE_TIFF_COMPRESSION), 50]
result_tiff, encimg_tiff = cv2.imencode('.tif', frame)

encode_param_webp = [int(cv2.IMWRITE_WEBP_QUALITY), 50]
result_webp, encimg_webp = cv2.imencode('.webp', frame, encode_param_webp)
print("frame size: ", frame.shape)

# #avif
# encode_param_avif = [int(cv2.IMWRITE_AVIF_QUALITY), 50]
# result_avif, encimg_avif = cv2.imencode('.avif', frame, encode_param_avif)

#comparethe size of the frame and encoded image

print("jpeg size: ", len(encimg_jpeg))
print("tiff size: ", len(encimg_tiff))
print("webp size: ", len(encimg_webp), "\n")

# decode the jpeg encoded image
decimg_jpeg = cv2.imdecode(encimg_jpeg, 1)
decimg_tiff = cv2.imdecode(encimg_tiff, 1)
decimg_webp = cv2.imdecode(encimg_webp, 1)

# merge the images   side by side
merged_image = np.hstack((decimg_jpeg, decimg_tiff, decimg_webp))

# Display the frame
cv2.imshow('frame', merged_image)
cv2.waitKey(1)  # Adjust as needed to display the frame

I am trying to reduce the amount of data in video transmission.

If you try to compress the image with the AVIF codec by activating lines 29-30 in the above code, the following error occurs.

result_avif, encimg_avif = cv2.imencode('.avif', frame, encode_param_avif)

cv2.error: OpenCV(4.8.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:1120: error: (-2:Unspecified error) could not find encoder for the specified extension in function ‘imencode’

I am using opencv-4.8.0 in a python3 environment.
I’m asking because I think there is a variable for the avif codec in the documentation.

that is not done by compressing pictures. that is done by video compression.

AVIF support is optional and requires the right libraries to be available when you build your OpenCV.

thanks for the information. I was using opencv-python package.
maybe I have to build it from source like old time.

AVIF support is extremely new. you’ll probably need libavif. I don’t know if the cmake stuff fetches that or requires you to provide it.

also looks like you’ll have to use VideoWriter, since that format is based on AV1, a video codec.