Search code examples
javaimagej

Have ImageJ automatically overwrite when using saveAs


I'm currently using the ij.jar in a Java application and using ImageJ to save some processed images to the disk. However, if I try to save an image to a file that already exists, the save fails silently. The original file remains untouched and ImageJ does not throw an error.

Here is a sample piece of code:

IJ.saveAs(image, filename);

Is there a way to have saveAs automatically overwrite if a file with the same name already exists?


Solution

  • I think you'll be best off using the FileSaver class (ij.io.FileSaver) instead of the generic saveAs method. For example, if your ImagePlus is image, then you can save it as a TIFF stack with:

    new FileSaver(image).saveAsTiffStack(path);
    

    If you need another format, have a look at the documentation, and you'll see that there are similar methods for other formats.