Ask Your Question
0

Error during image decoding (imdecode) using python

asked 2013-07-30 10:49:59 -0600

JR_ gravatar image

updated 2013-07-30 10:52:29 -0600

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.

edit retag flag offensive close merge delete

Comments

hmm, strange. tested with 2.4.2 and 2.9.0 , py2.7, xp. - no prob here

berak gravatar imageberak ( 2013-07-30 11:05:54 -0600 )edit

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])

JR_ gravatar imageJR_ ( 2013-07-30 13:31:18 -0600 )edit

1 is the correct value, so no cigar ;(
you tried skipping the whole encode_param block ?

result, encimg = cv2.imencode('.jpg', img)

berak gravatar imageberak ( 2013-07-30 13:40:14 -0600 )edit

Hi berak,

Then I get :

TypeError: Required argument 'buf' (pos 3) not found

JR_ gravatar imageJR_ ( 2013-07-30 14:32:49 -0600 )edit

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

berak gravatar imageberak ( 2013-07-30 14:38:27 -0600 )edit

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?

JR_ gravatar imageJR_ ( 2013-07-31 08:15:07 -0600 )edit

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)

berak gravatar imageberak ( 2013-07-31 08:24:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-08-07 17:35:56 -0600

JR_ gravatar image

updated 2013-08-07 18:59:46 -0600

Problem solved!! I removed everything related to open cv and reinstalled it..... It seemed to be using opencv from simplecv....

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-07-30 10:49:59 -0600

Seen: 7,585 times

Last updated: Jul 30 '13