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

maurog qualsivoglia at dovetipare.nz
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
flags = cv2.CV_LOAD_IMAGE_COLOR
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 

https://stackoverflow.com/questions/13329445/how-to-read-image-from-in-
memory-buffer-stringio-or-from-url-with-opencv-pytho

r = open('bad.jpg','rb').read()
img_array = np.asarray(bytearray(r), dtype=np.uint8)
flags = cv2.CV_LOAD_IMAGE_COLOR
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