Ask Your Question

can't imread or imwrite in python

asked 2013-03-13 05:12:39 -0500

berak gravatar image

updated 2013-05-25 04:33:50 -0500

my first go at the python api (on win, 2.4.9), built cv2.pyd, but i can't imread ( result always None) or imwrite ( "could not find a writer for the specified extension" )

checked the pyd with dependancy walker, it's using the same dlls as my c++ code does (no problem there, png, jpg support built in ) other stuff in python seems to work well, e.g opening a videocapture / imshow

Edit: oh, fun, it turns out all strings i pass into the opencv api come out empty.

indeed, imwrite can't find a writer for 'empty string', but why doesn't it tell me ?

and why does it happen in the first place ?

using VS2008 here, python2.7.1 suspecting strange string settings, unicode woes, python compiled against a different runtime, hell, that's the stuff i hate most ;(

Edit: Problem solved ( finally ...)

had to choose /D "UNICODE" ( unicode support in the general settings )

most of the std::strings in opencv have been replaced with cv::Strings, i guess they only work with unicode ( when combined with python )

edit retag flag offensive close merge delete


Could it be a problem with relative and absolute paths?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-13 05:21:48 -0500 )edit

thanks for the hint, but i think - no.

cv2.error: ..\..\..\modules\highgui\src\loadsave.cpp:269: error: (-2) could not find a writer for the specified extension

does not look like a path problem, right ?

error is even coming from the opencv library, and that's the same i'm using from c++

berak gravatar imageberak ( 2013-03-13 05:36:07 -0500 )edit

could you post the exact imwrite command you are using? to me this error means that you are trying to write a specific type of image compression or so that is not recognized by the python implementation.

Just to illustrate my opinion: using imwrite("d:\test.jpg",image) in C++ defines which type of imageconversion it needs to use based on the .jpg extension in the file name. So knowing what extension you using, could locate the problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-13 05:44:28 -0500 )edit

(posting code in comments here is a PITA)

thanks for breaking your head, btw ;)

berak gravatar imageberak ( 2013-03-13 05:49:30 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-13 05:59:52 -0500

updated 2013-03-13 06:43:41 -0500

import cv2

im0 = cv2.imread("E:/code/opencv/p/piiple/image.png");  
print im0  ### None; I think this holds the actual problem;

cap = cv2.VideoCapture(0)
while( cap.isOpened() ) :
   ret,img =
   cv2.imwrite("lll.png",img)   ### see error at QA
   k = cv2.waitKey(10)
   if k == 27:

Looking at the code, isn't it the capturing that is conflicting? Meaning that it doesnt capture a correct frame from your webcam and sendint it towards the img object?

edit flag offensive delete link more


  1. you mean the print statement ? applied on an img from the capture, it shows pixels, and the numpy-array type.
  2. imshow() likes it(shows a nice pic), i can print it( and see pixels, correct type) so, the img should be valid

i'm more thinking, that i've done something wrong, when building the python module

(or numpy even, did not bother to get nose, to test it)

berak gravatar imageberak ( 2013-03-13 06:19:23 -0500 )edit

What i mean is the following 1. You read in an image from an exact location. 2. You print the MAT object, which should just be an adress in memory that is outputted 3. You open a videocapture from default device (webcam) 4. While there is a capture possibility, you read an image from the feed and show it.

Wanted to know if the read stream fram was actually valid, but if the imshow does the correct think, then it should be fine.

Could it be the problem that img isn't explicitly defined as a variable? Don't even know if that is required in Python... Just trying some guesses

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-13 06:40:43 -0500 )edit

ok, bad idea to put both(read/write) cases into the same testsample. sorry for the confusion. but they show up even when separated.

no, you don't need to declare variables, or their types in python (duck typing, it's all done in the assignment)

afaik, in cv2, there's not even an image type(like the IplImage wrapper in cv 1.0) anymore, but it's numpy arrays all the way down instead

berak gravatar imageberak ( 2013-03-13 06:52:14 -0500 )edit

Could you try the following code? And tell me if it does the trick?

// create a parameter vector and assign explicitly


second value = 0;

Then perform this command

cv2.imwrite(filename, img, params)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-13 06:59:30 -0500 )edit

cv2.imwrite("lll.png",img,[16,0]) still same. it des not even work with pgm or ppm or bmp

berak gravatar imageberak ( 2013-03-13 07:08:21 -0500 )edit

Im sorry, but this is about all the inspiration I have right now :) Guess we need someone else to provide some insight. Checked reported bugs, but none of them match this problem...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-13 07:16:53 -0500 )edit

yes, thanks for going through all 'common sense' stuff, though

berak gravatar imageberak ( 2013-03-13 07:28:46 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-03-13 05:12:39 -0500

Seen: 7,384 times

Last updated: May 25 '13