Ask Your Question

Is there a way to keep imwrite from overwriting files..?

asked 2015-06-30 07:16:25 -0500

mcsplace gravatar image

Using the latest OpenCV 3.0 on a Linux platform. Using the following to write out image files. However it is more than happy to overwrite an existing file... Is there a mechanism to keep imwrite() from overwriting files...?

/* Create parameters */
vector<int> params;
if( "png" ) == 0 )
    params.push_back( IMWRITE_PNG_COMPRESSION );
    params.push_back( png_compress_level );

/* Try to write image */
    imwrite( filename, *img, params );
catch (runtime_error& ex) 
    fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
    return( 0 );
edit retag flag offensive close merge delete



Yes, you shall do some kind of image name generator, so you'll have a different filename each call of imwrite. For eg: int i=0; std::string savingName = filename + std::to_string(i) + extension;

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-06-30 08:55:29 -0500 )edit

//to check if the file exist

if (!cvHaveImageReader(filename))
sturkmen gravatar imagesturkmen ( 2015-06-30 09:10:35 -0500 )edit

@sturkmen: but this way, it will not save the image, if it exists...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-06-30 09:40:34 -0500 )edit

I am using a name generator for the image filename... Problem is that I forget to move the files after a run.. Then I start up the application again and overwrite my previous run...

I have tried the cvHaveImageReader() and it works as @sturkmen stated above... However, is there any documentation on this function...? I can't find anything.. Thanks,

mcsplace gravatar imagemcsplace ( 2015-06-30 10:10:23 -0500 )edit

@thdrksdfthmn you are right. i only wanted to mention that we can use cvHaveImageReader(filename) to determine if the file exist. int cvHaveImageReader ( const char * filename ) determine can opencv open an image file or not.

sturkmen gravatar imagesturkmen ( 2015-06-30 10:27:35 -0500 )edit

Why C-API???And more, you shall use cvHaveImageWriter, no? There is no doc for it...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-07-01 02:35:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-07-02 20:35:10 -0500

look a sample code below. for more information you can look here

#include "opencv2/imgcodecs.hpp"
#include <iostream>

using namespace cv;

static bool isFileExist( const String& filename )
    /// Open the file
    FILE* f= fopen( filename.c_str(), "rb" );

    /// in the event of a failure, return false
    if( !f )
        return false;


    return true;

int main(int /*argc*/, char** /*argv*/)
    Mat test = Mat::zeros(100, 100, CV_8UC1);

    String filename = "test.jpg";

    if( !isFileExist(filename))
        imwrite(filename, test);
    else std::cout << filename  << " exist. not saved" << std::endl;

    return 0;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-30 07:16:25 -0500

Seen: 4,581 times

Last updated: Jul 02 '15