Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError using cv.SaveImage in openCV

Tags:

python

opencv

I want to convert a jpg file to png, but when I run this code :

from opencv import _cv    
from opencv.highgui import cvSaveImage, cvLoadImage

cvSaveImage("bet.jpg",cvLoadImage("bet.jpg")) 

if __name__ == '__main__':
    pass

It gives this error which I don't understand :

Traceback (most recent call last):
  File "convert.py", line 6, in <module>
    cvSaveImage("bet.jpg",cvLoadImage("bet.jpg")) 
  File "/usr/lib/pymodules/python2.6/opencv/highgui.py", line 183, in cvSaveImage
    return _highgui.cvSaveImage(*args)
RuntimeError:  openCV Error:
        Status=Null pointer
        function name=cvGetMat
        error message=NULL array pointer is passed
        file_name=cxarray.cpp
        line=2780

I have my picture with the same folder of source code and the name of the image is bet.jpg

Any idea ??

like image 315
iva123 Avatar asked Mar 17 '10 13:03

iva123


3 Answers

The best choice is pyopencv:

import pyopencv as cv

img = cv.imread('01.png')

cv.imshow('img-windows',img)
cv.waitKey(0)
cv.imwrite('01.png',img)
like image 133
rosickey Avatar answered Sep 19 '22 23:09

rosickey


From Python CV documentation, the CV2 method for converting a jpeg to png is: Python: cv2.imwrite(filename, img[, params]) → retval

For my example:

import cv2
filename = 'pic.jpeg'
cam = cv2.VideoCapture(filename)
s, img = cam.read()
picName = 'pic.png'
cv2.imwrite(picName, img)

VideoCapture is nice and general, and works with videos, webcams and image files.

like image 15
Kelton Temby Avatar answered Sep 22 '22 23:09

Kelton Temby


I solved the problem, the image I took randomly from the Google Images doesn't load. Maybe it's encrypted or something I don't know. I tried it with other images, and worked very well. So watch out while copying images : )

like image 3
iva123 Avatar answered Sep 19 '22 23:09

iva123