Bug on closing ImagePlus without save dialog after adding ROI in javascript?



I encountered a weird issue while writing a script in javascript that doesn’t allow me to close an ImagePlus without the save dialog popping up, despite having set the ‘changes’ field to false. Here is a minimal set of code lines that reproduce the phenomenon:


var image = NewImage.createFloatImage("Untitled", 1000, 1000, 1, NewImage.FILL_BLACK);

var roi = new PointRoi(new FloatPolygon([0],[0]));
roi = roi.addPoint(1, 1);
roi = roi.addPoint(2, 2);

image.setRoi(roi, true);
image.changes = false;

Note: the second point addition is required to trigger the issue. I’m on Fiji in MacOs latest version of all software.


Nice catch, @lollopus, thanks for reporting!

I suppose you see that behavior because of the following lines in ImageWindow.java:

You can work around the issue by adding the following line before adding the ROI to the image:


This method was apparently introduced recently with this commit:

but the reason why it was added is unclear to me.

I’d consider the behavior a bug, as it behaves inconsistently depending on how often roi.addPoint() was called.

@Wayne do you agree?


Hi and thanks for the effective workaround! I’m glad I could help a little by spotting this one… cheers, Lorenzo


This 1.52c regression is fixed in the latest ImageJ daily build (1.52e25). The “Save changes?” dialog is now only displayed when closing an image that has a point selection with more than 10 points added while the image is showing. The dialog is displayed to prevent work from being lost when using the multi-point tool as a counter.

The source code changes can be viewed at


Thanks for the credits Wayne… Just a question: In my example above I used 3 points, but my actual script dynamically generates a much larger multi point selection based on the interpolation of freelines drawn by the user. Will I be able to avoid the save dialog when programmatically closing the image by setting img.changes=false or will I need to keep using the workaround suggested by Imagejan? Thanks L


You will not need the workarounds if you upgrade to the daily build. This example creates a selection with 5000 points:

  image = IJ.createImage("Untitled", "32-bit", 1000, 1000, 1);
  ran = new Random()
  roi = new PointRoi(new FloatPolygon([0],[0]));
  for (i=0; i<5000; i++) {
     x = ran.nextDouble()*1000;
     y = ran.nextDouble()*1000;
     roi.addPoint(x, y);


Excellent, thanks again!


The latest daily build (1.52e27) adds a no-argument constructor to the PointRoi class, making it easier to create multi-point selections. It also adds a size() method, which is equivalent to getPolygon().npoints. The following example creates a multi-point selection with 5000 points. The previous example actually created a 5001 point selection due to the point added by the constructor.

  size = 700;
  npoints = 5000;
  ran = new Random()
  points = new PointRoi();
  for (i=0; i<npoints; i++) {
     x = size/2+ran.nextGaussian()*size/7;
     y = size/2+ran.nextGaussian()*size/7;
     points.addPoint(x, y);
  img = IJ.createImage("tmp", "16-bit", size, size, 1);
  print("points: "+points.size());