Catch exception from ofSaveImage()

Hi, I would like to catch an exception from the ofSaveImage() object: which receives an ofImage object and a string object for the path:

    // I'm using a Mac
    //image is of type ofxCvGrayscaleImage

    string path = "/Users/.../Desktop/fileDump/" + ofGetTimestampString();

          ofSaveImage(image, path);
     catch(ofImage &e){
          cout << "unable to write file" << endl;

I’m quite new to OF and C++ . I’m not seeing the exceptions cout message but no image is saved either.

OF doesn’t throw exceptions, instead errors are usually returned as return values, in the case of ofSaveImage there’s not error value (it should have one i’ll try to correct it for next release) so unless you get a message on the console there’s currently no way to know if there was an error.

I would use an ofPixels instead of ofxCvGrayScaleImage but some image formats don’t support grayscale or rgba, like for example jpg so if your image is grayscale try converting it first to rgb before saving or using a different format

1 Like

Thanks arturo for you time in getting back and for the fast response. Very helpful thank you!