Error during image decoding (imdecode) using python
I use puthon 2.7, windows 7 and opencv 2.4.6. and I try to run the following code:
https://github.com/kyatou/python-opencv_tutorial/blob/master/08_image_encode_decode.py
#import opencv library
import cv2
import sys
import numpy
argvs=sys.argv
if (len(argvs) != 2):
print 'Usage: # python %s imagefilename' % argvs[0]
quit()
imagefilename = argvs[1]
try:
img=cv2.imread(imagefilename, 1)
except:
print 'faild to load %s' % imagefilename
quit()
#encode to jpeg format
#encode param image quality 0 to 100. default:95
#if you want to shrink data size, choose low image quality.
encode_param=[1, 90]
result,encimg=cv2.imencode('.jpg',img,encode_param)
if False==result:
print 'could not encode image!'
quit()
#decode from jpeg format
decimg=cv2.imdecode(encimg,1)
cv2.imshow('Source Image',img)
cv2.imshow('Decoded image',decimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
I keep getting the following error:
result, encimg = cv2.imencode('.jpg', img, [1, 90])
TypeError: 'bool' object is not iterable
I have tried a lot of things: reinstall opencv, convert cv2 to cv code and searched different forums but I keep getting this error. Am I missing something? Is there someone who can run this code without getting the error?
BTW: Other opencv code (taking pictures from webcam) runs without problems....
At the moment I save the image to a temp JPG file. Using the imencode function I want to create the jpg file in the memory.
Thanks in advance and with best regards.
hmm, strange. tested with 2.4.2 and 2.9.0 , py2.7, xp. - no prob here
As an additional note: First I had problems with the following piece of code:
encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), 90] AttributeError: 'module' object has no attribute 'IMWRITE_JPEG_QUALITY'
I fixed this using the following code:
result, encimg = cv2.imencode('.jpg', img, [1, 90])
1 is the correct value, so no cigar ;(
you tried skipping the whole encode_param block ?
result, encimg = cv2.imencode('.jpg', img)
Hi berak,
Then I get :
TypeError: Required argument 'buf' (pos 3) not found
hey, that's good even. getting closer to the problem. 'buf' is actually, what should be returned as 'encimg', so why does it expect it as 3rd arg ? there must be some diff between 2.4.2(what the tut expected) and 2.4.6
I've also tried another system (also Win 7) and here it also doesn't work... Ik can try XP and Ubuntu? Can it be an OS dependent problem?
i'm a bit out of ideas, but: imread does not throw an exception on failure, but just returns None. (and ofc, that will crash imencode, so do yourself a favour and check that)