Need help with error concerning processing of several pictures in a row (macro)

macro
roi
Tags: #<Tag:0x00007fd540b507a8> #<Tag:0x00007fd540b50668>

#1

<Hello everyone,

I wrote a macro that creates grayscale plots of a specific area of pictures. It is supposed to do so with all of the pictures in the selected source-folder.
Now my problem is, that the macro sometimes works perfectly fine and creates the grayscale plots of all pictures. Then at other times it just stops at a radom picture and displays the following error: “This command requires a TextWindow, such as the “Log” window or an “Editor” window. Use File>Save>Text Image to save an image as text.”

I have no clue why this happens, especially because sometimes the macro works perfectly fine! So I would appreciate any help!

Here is the code:
macro “Grauwerteplot Action Tool - C0f0L1fffL1f11T5d14G”{

//Quellordner bestimmen
dir_Quelle = getDirectory("Quellordner festlegen");

//Zielordner festlegen
dir_Ziel = getDirectory("Zielordner festlegen");

//Anzahl Bilder im Quellordner bestimmen
list = getFileList(dir_Quelle);
anzahl = list.length;

//Bilder im Ablageordner nacheinander öffnen und Grauwerteplot erstellen

for (i=0; i<anzahl; i++){
	//Makro im Hintergrund laufen lassen
	setBatchMode(true);

	//i-tes Bild in Ordner öffnen
	open(dir_Quelle + list[i]);

	//Bildname bestimmen
	title = getTitle();

	//Bildhöhe und Breite bestimmen
	width = getWidth;
	height = getHeight;

	//Startpunkte für Rechteckauswahl festlegen
	height_atomizer = 86;
	y_start = 61;
	x_start = 350;
	width_1 = 250;

	//rechteckigen Bildausschnitt wählen
	makeRectangle(x_start, y_start, width_1, height_atomizer);

	//8-bit Grauwertebild erstellen
	run("8-bit");

	//Grauwerteplot erstellen
	run("Plot Profile");
	Plot.getValues(x,y);
	for (k=0; k<width_1; k++)
	print (x[k], y[k]);
	saveAs("Text", dir_Ziel+title);

	//geöffnete Fenster schließen
	close();
	run("Close");

}


#2

Good day Judith,

I didn’t look into the details of your macro but using

setBatchMode(true);

the way you do, isn’t recommended.

You should in any case put it before the loop and after the loop use

setBatchMode(false);

Furthermore, try to run the macro without both “setBatchMode” commands and report back about the issue.

Regards

Herbie


#3

EDIT: misunderstood you, but maybe you need to select the text window before saving, it might be trying to save your image window as a text file instead?

Try:

selectWindow("Log");
saveAs("Text", dir_Ziel+title);

#4

Judith,

this version may help:

//Quellordner bestimmen
dir_Quelle = getDirectory(“Quellordner festlegen”);

//Zielordner festlegen
dir_Ziel = getDirectory(“Zielordner festlegen”);

setBatchMode(true);

//Anzahl Bilder im Quellordner bestimmen
list = getFileList(dir_Quelle);
anzahl = list.length;

//Startpunkte für Rechteckauswahl festlegen
height_atomizer = 86;
y_start = 61;
x_start = 350;
width_1 = 250;

for (i=0; i<anzahl; i++){

//i-tes Bild in Ordner öffnen
open(dir_Quelle + list[i]);

//Bildname bestimmen
title = getTitle();

//Bildhöhe und Breite bestimmen
width = getWidth;
height = getHeight;

//rechteckigen Bildausschnitt wählen
makeRectangle(x_start, y_start, width_1, height_atomizer);

//8-bit Grauwertebild erstellen
run(“8-bit”);

//Grauwerteplot erstellen
run(“Plot Profile”);
Plot.getValues(x,y);
for (k=0; k<width_1; k++) {
print (x[k], y[k]);
}
//geöffnete Bild-Fenster schließen
close();
close();

selectWindow(“Log”);
saveAs(“Text”, dir_Ziel+title);
run(“Close”);
}

setBatchMode(false);

Please note that this version still is suboptimum because the Log-window isn’t hidden. Try to save the data directly.

Best

Herbie


#5

Judith,

here is a streamlined version of your macro:

//Quellordner bestimmen
dir_Quelle = getDirectory("Quellordner festlegen");

//Zielordner festlegen
dir_Ziel = getDirectory("Zielordner festlegen");

setBatchMode(true);

//Anzahl Bilder im Quellordner bestimmen
list = getFileList(dir_Quelle);
anzahl = list.length;

//Startpunkte für Rechteckauswahl festlegen
height_atomizer = 86;
y_start = 61;
x_start = 350;
width_1 = 250;

for (i=0; i<anzahl; i++){

	//i-tes Bild in Ordner öffnen
	open(dir_Quelle + list[i]);

	//Bildname bestimmen
	title = File.nameWithoutExtension;

	//Bildhöhe und Breite bestimmen
	width = getWidth;
	height = getHeight;

	//rechteckigen Bildausschnitt wählen
	makeRectangle(x_start, y_start, width_1, height_atomizer);

	//8-bit Grauwertebild erstellen
	run("8-bit");

	//Profilwerte ermitteln und in Text-Datei schreiben
	p = getProfile();
	t = "";
	for (k=0; k<p.length; k++) {
		t += "" + k + "\t" + p[k] + "\n";
	}
	File.saveString( t, dir_Ziel + title + ".txt" );

	//Bild-Fenster schließen
	close();

}

setBatchMode(false);

HTH

Herbie


#6

Hey Herbie,

thanks for answering so quickly! Both versions (the one with setBachtMode(false) at the end and the one that hides the Log-window) worked the first time I ran them.
I will be using them the next couple of days and will check if the error still occurs.

I am new to writing macros for ImageJ, so this is my first one. I don’t really get how the “Profilwerte ermitteln und in Text-Datei schreiben” part works. I see the result in the txt-file but I don’t understand the code.

Judith


#7

Well Judith,

here

https://imagej.nih.gov/ij/developer/macro/functions.html

you should find all ImageJ-macro commands with explanations and some links to example code. Just look-up the commands that you don’t yet understand.

Regards

Herbie