Interpreting imencode()[1]

If i use cv2.imencode(’.jpg’,img) and then use image = cv2.imdecode(img_encode, cv2.IMREAD_COLOR) i get the error like “TypeError: buf is not a numerical tuple” but if cv2.imencode(’.jpg’,img)[1] is used then it shows the frame correctly. So, what does cv2.imencode(’.jpg’,img)[1] imply?

import numpy as np
import cv2

imgg=cv2.imread(‘filename1_dec.jpg’)
img_encode=cv2.imencode(’.jpg’,img)[1]

image = cv2.imdecode(img_encode, cv2.IMREAD_COLOR)
cv2.imshow(‘img_decode’,image)
cv2.waitKey()

Hi,
imencode result is a tuple so you should write

ret, img_encode=cv.imencode('.jpg', img)
if ret:
    image = cv.imdecode(img_encode,  cv.IMREAD_COLOR)
    cv.imshow('img_decode', image)
    cv.waitKey()  
else:
    print("can't encode image")

Good practise is always check status return

1 Like

Thank you so much for your reply