OpenCv: different behavior of cv2.imread and cv2.imdecode

maurog qualsivoglia at
Sat Jan 24 11:48:02 CET 2015

The starting point is an invalid jpg file, grabbed from a site showing 
pictures from a webcam while the site is updating the picture itself. So 
the upper part of the picture is ok, the lower part no.

If I execute this code
img = cv2.imread('bad.jpg', flags)

this message is printed on stderr

"Premature end of JPEG file"

and in the part of img np array related to the lower part of the picture 
all the values are 128 for all the RGB layers, that is black pixels.

If I execute this code, as suggested here

r = open('bad.jpg','rb').read()
img_array = np.asarray(bytearray(r), dtype=np.uint8)
img = cv2.imdecode(img_array, flags)

no error message is shown and in the img np array there are mixed values 
in the part related to the lower part of the picture

So it looks to me that cv2.imread and cv2.imdecode handle in a different 
way the loading of a jpg image. Is it like that or am I missing something?

More information about the Python-list mailing list