IOException when saving a tiff

Hi everyone,

I am working on a quite big macro, which contains the following function.

function generation_seuils_masks(path){
    	run("Options...", "iterations=1 count=1 do=Nothing");
    	i=0;
    	while(i<=(couche-1)){
    		j=i*2;
    		selectWindow("AOI0.tiff");
    		run("Duplicate..."," AOI ");
    		rename(Saturation[i]+"%"+"_" +Saturation[i+1]+"%"); // OK
    		titreP=getTitle();
    		run("Threshold...");
    		setThreshold(Itab[j],Itab[j+1]);
    		run("Convert to Mask");
    		run("Close");
    		lien=path+"carte\\"+titreP+".tiff";
    		selectWindow(titreP);
    		saveAs("Tiff",lien); //ERROR HERE
    		run("Set Measurements...","area mean redirect=AOI0.tiff decimal=6");
    		run("Analyze Particles...","display clear");
    		saveAs("Results",path+titreP);
    		i=i+1;
    	}
    	selectWindow("AOI0.tiff");
    	close();	
    }

In this step, I threshold an image depending on the grey values of each pixel. Each grey value is linked to the quantity (in %) of a liquid in glass beads. The saturation array refers to the real quantity of liquid in this pixel. The Itab array refers to the corresponding threshold value.

This macro has worked well without problem for several weeks but I get now this error.

I modified other parts of the main macro but I don’t change this function.
I don’t know if it is linked to the problem, but I don’t understand why the second line is truncated. “lab” should be “labo”, as written in the third line.

Does anyone have an idea?

Thanks,

Regards

Fabien

Good day Fabien,

please make sure that all inverted commas are not of the curly kind.
If the error persists, then please post a minimum example that we can test.

Regards

Herbie

Herbie,

Thanks for your quick answer. I don’t understand your first sentence. What does mean “are not of the curly kind”. (sorry for this question…)

Thanks

Please check the code you’ve posted. It contains two different kinds of inverted commas.

" "

and

“ ”

The second kind won’t work.

HTH

Herbie

Herbie,

It is due to the copy/paste in the forum. In the editor, everything looks good.

My picture seems to be too big. :frowning:
You can download here:
https://wetransfer.com/downloads/53a2c4798c6215d40f7c0890b31247bb20180205133505/1979e4335d19fc285e481eb40994bda520180205133505/efae11

As the array are created before in the macro use the following values:
For Saturation=newArray(100,60);
Itab=newArray(0,14);
And couche = 1 for this test

The software that is running this forum (Discourse) automatically transforms quotes (") into typographic quotes (“”) in your posts unless you use proper code formatting (with the </> button, or using “code fences” (```).
I edited your above post so that the code is displayed correctly. The source code of your function doesn’t seem to have any problem with curly quotes, they all seem to be correct.

2 Likes

I fear you are closing the wrong image, not “AOI0.tiff”.

Regards

Herbie

If I transform the function in a macro, as the following, everything works… I suspect a problem with the “path” variable.

macro generation_seuils_masks{
	Saturation=newArray(100,60);
	Itab=newArray(0,14);
	couche=1;
	path="D:\\Documents\\user\\Travail\\1-Projets_en_cours\\project_name\\Test_traitement_photo\\11-reflexion_oeillets\\labo\\5.fusion\\P03533\\";
	run("Options...", "iterations=1 count=1 do=Nothing");
	i=0;
	while(i<=(couche-1)){
		j=i*2;
		selectWindow("AOI0.tiff");
		run("Duplicate..."," AOI ");
		rename(Saturation[i]+"%"+" " +Saturation[i+1]+"%"); // OK
		titreP=getTitle();
		run("Threshold...");
		setThreshold(Itab[j],Itab[j+1]);
		run("Convert to Mask");
		run("Close");
		lien=path+"carte\\"+titreP+".tif";
	selectWindow(titreP);
	saveAs("Tiff",lien); //ERROR HERE
		run("Set Measurements...","area mean redirect=AOI0.tiff decimal=6");
		run("Analyze Particles...","display clear");
		saveAs("Results",path+titreP);
		i=i+1;
	}
	selectWindow("AOI0.tiff");
	close();	
}

Same problem if I comment the run(“Close”) line (which closes the Threshold windows. The AOI windows is first duplicated).

I check the path variable with waitForUser(path); The result is correct. It is so strange…

Two possible issues:

  1. “carte” appears to be an existing folder. Does it really exist?
  2. Make sure you are not closing the image that you like to save.
    When I try your code, the duplicated image is closed.

HTH

Herbie

Herbie,

Yes the carte folder exist. It is created at the beginning of the whole macro.

For your second suggestion, did you try the last code (macro)?

Thanks

Yes, I tried the macro.

I have copy/paste my macro, without change the path (ie “user” and “project_name” are not the real string) and I get the same IOexception error.

If I correct by the right strings, everything works fine. (Fiji 1.51s, Win 10, 64 bits)
The AOI0.tiff is closed at the end but it is normal. The encountered problem happened in the while loop: at the moment, AOI0 is still opened.

Could it be a trouble with “\” (double \ but it does not appear correctly on the forum) and “/” in paths?

I’m on Mac and your macro works as expected if I omit the “close()” command.

Please check the logic of your code.

If you doubt your path string you may use

path = getDirectory("image");

instead which uses the folder from which the last image was opened.

Regards

Herbie

Herbie,

I try something else:
Instead of saveAs(“Tiff”,lien), I write saveAs(“Tiff”);

As such, a dialog windows opened. The proposed folder was the “5.fusion” folder. I open the “P03533” folder, then the “carte”’ folder. I write manually the name of the file “100% 60%”, press OK in order that the picture is created. The following windows appears, even if the picture has been created in the folder, as you can see below.
Sans titre2

Would you have any idea?

I have nothing to add to my previous posts: It works for me without the “close()”-command.
Just check the logic of your code.

Have success

Herbie

Herbie,

Thanks for your answer. In my last post, I encountered a problem that seems to be not linked with my code. My macro opened the SaveAsTiff dialog bug. I browse to the right folder. I save the tiff in this folder. That works, as the tiff is created, but at the same time, I get the error dialog window. Could it be a bug in the saveAsTiff command?

But as you proposed, I will check the logic of my code.

Regards,

In the previous screenshot, the error was due to an error in “saveAs(“Results”,path+titreP)” and not with SaveAs(“Tiff”,lien).
Herbie, the close() command is outside the while loop in which the problem occurs. So for me, there is no link between my problem and this close command. As a proof, if I write “saveAs(“Tiff”)” and saveAs(“Results”), instead of “saveAs(“Tiff”,lien)” and “saveAs(“Results”,path+titreP)”, in order to have dialog boxes, the macro works like a charm : tiff and results table are save properly in the selected folders.

So I understand from these tests that the problem is linked with the paramater “lien”. and “path+titreP”

lien and titreP are defined as variable at the beginning of the whole macro.
If I print the “lien”, “path” and “titreP” variable just before the saveAs command (as below), everything seems to be in order. I don’t understand where is the problem.

Thanks for any help

Maybe this helps:

save(path)
Saves an image, lookup table, selection or text window to the specified file path. The path must end in “.tif”, “.jpg”, “.gif”, “.zip”, “.raw”, “.avi”, “.bmp”, “.fits”, “.png”, “.pgm”, “.lut”, “.roi” or “.txt”.

saveAs(format, path)
Saves the active image, lookup table, selection, measurement results, selection XY coordinates or text window to the specified file path. The format argument must be “tiff”, “jpeg”, “gif”, “zip”, “raw”, “avi”, “bmp”, “fits”, “png”, “pgm”, “text image”, “lut”, “selection”, “results”, “xy Coordinates” or “text”. Use saveAs(format) to have a “Save As” dialog displayed.