图像解码时出错(imdecode)

时间:2013-07-30 11:45:20

标签: opencv python-2.7

我使用puthon 2.7,windows 7和opencv 2.4.6。我尝试运行以下代码:

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=[int(cv2.IMWRITE_JPEG_QUALITY),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() 

我一直收到以下错误:

encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), 90]
AttributeError: 'module' object has no attribute 'IMWRITE_JPEG_QUALITY'

我尝试了很多东西:重新安装opencv,将cv2转换为cv代码并搜索不同的论坛,但我一直收到此错误。我错过了什么吗?是否有人可以运行此代码而不会收到错误?

BTW:其他opencv代码(从网络摄像头拍照)运行没有问题....

目前我将图像保存为临时JPG文件。使用imencode函数我想在内存中创建jpg文件。

提前致谢并致以最诚挚的问候。

1 个答案:

答案 0 :(得分:1)

问题不在你的代码中,它应该可以工作,但它与你的OpenCV Python包有关。我无法告诉你为什么会提出这个错误,但你可以通过改变encode_param声明的行来避免它:

encode_param=[1, 90]