0

I am using this function

def data_uri_to_cv2_img(uri):
    encoded_data = uri.split(',')[1]
    nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

to get cv2 images starting from a base64 encoded string. I got the code from this stackoverflow answer.

However, I get an error in this particular case:

src_val = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7"

img = data_uri_to_cv2_img(src_val)
cv2.imshow('ay', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

which causes this error:

Traceback (most recent call last):   File "playground.py", line 14, in
<module>
    cv2.imshow('ay', img) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-sxpsnzt6\opencv\modules\highgui\src\window.cpp:376:
error: (-215:Assertion failed) size.width>0 && size.height>0 in
function 'cv::imshow'

Where is the issue stemming from? Is the string poorly formatted? Is it because it encodes a gif? Are there limitations to this function?

Luca Guarro
  • 1,085
  • 1
  • 11
  • 25

1 Answers1

0

the picture is a 1x1 pixel transparent GIF.

imdecode() returns None.

try VideoCapture() instead. OpenCV seems to understand GIFs as a video format... GIFs can be a sequence of pictures so that makes sense.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36