Discussion:
Writing a macro to save image stack as series of separate files
jlw
2014-09-10 22:51:01 UTC
Permalink
Hi, I'm trying to write a macro to open image stacks, manually look at them
and adjust brightness/contrast, then save as a series of separate files,
each in its own folder. The way the files are named is not flexible because
they have to be named in a certain way to feed into another Matlab program
that is already written. Here is the code I have:


function prepareForMatlab(input, output, filename) {
setBatchMode(false);
open(input + filename);
run("Brightness/Contrast...");
waitForUser('Adjust brightness and contrast');
run("Close"); //The filename is automatically extracted but we have to
modify this to get it without the extension
name = getTitle; //Includes .tif
dotIndex = indexOf(name, ".");
title = substring(name, 0, dotIndex); //name without the extension
mouseAndRegionNumber = substring(output,33); //Extracts the mouse and
region number, in this case delta_1. Will change if file structure changes
print(mouseAndRegionNumber);
newFileName = mouseAndRegionNumber + "_" + title + "_";
print(newFileName);
firstPlaneName = output + "/" + title + "/";
print(firstPlaneName);
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName");

run("Close All");
}

input = "/C:/.../Raw/"; //Here i've omitted some directories with a ... just
for the purpose of posting this question
output = "/C:/.../delta_1";

list = getFileList(input);
for (i = 0; i < list.length; i++){
setBatchMode(false);
prepareForMatlab(input, output, list[i]);
}


I know this code is probably not the most elegant way to do this (accepting
suggestions on that as well!) but the problem I'm having now is that in the
line
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName")
newFileName is not treated as a variable with a string value but rather as a
string itself (so all the files end up having the word newFileName in them).
How do I fix this?

This code also has another weird problem where the images open one by one
and allow the user to move the sliders to see different z planes and then
adjust brightness/contrast. This works fine unless you try to switch back
and forth between toggling z planes and toggling brightness. (Then the macro
gives and error "no image open") No idea why this would happen...it works
fine if you do one and then the other, just not if you switch back and
forth.

Any help would be greatly appreciated!!!



--
View this message in context: http://imagej.1557.x6.nabble.com/Writing-a-macro-to-save-image-stack-as-series-of-separate-files-tp5009571.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Volko Straub
2014-09-11 06:13:10 UTC
Permalink
In order for newFileName to be recognised as a variable inside this string, you need to write it as

run("Image Sequence... ", "format=TIFF name="+newFileName+" start=0 digits=3
save="+firstPlaneName)

Note the "+ before the variable and the +" after the variable - this will insert the content of your variable into the string.
I assume firstPlaneName at the end of that command is also a varibale. So, you will have to add "+ before that variable name as well. You don't need a +" at the end as there is no more text to be added to that string.

Below is an extract from the ImageJ Macro documentation that gives more details

The input fields of a dialog can be set to the contents of a macro
variables by using string concatentation
(http://rsb.info.nih.gov/ij/developer/macro/macros.html):

noise = 50; output = "Point Selection"; run("Find Maxima...",
"noise="+noise+" output=["+output+"] light"); With ImageJ 1.43 and
later, there is an easier method that only requires adding "&" to the
variable name in the options string: noise = 50; output = "Point
Selection"; run("Find Maxima...", "noise=&noise output=&output light");
String variables passed using this method do not have to be enclosed in
brackets. For more examples, see theArgumentPassingDemo
<http://rsb.info.nih.gov/ij/macros/ArgumentPassingDemo.txt>macro.

I hope this helps,
Volko
Post by jlw
Hi, I'm trying to write a macro to open image stacks, manually look at them
and adjust brightness/contrast, then save as a series of separate files,
each in its own folder. The way the files are named is not flexible because
they have to be named in a certain way to feed into another Matlab program
function prepareForMatlab(input, output, filename) {
setBatchMode(false);
open(input + filename);
run("Brightness/Contrast...");
waitForUser('Adjust brightness and contrast');
run("Close"); //The filename is automatically extracted but we have to
modify this to get it without the extension
name = getTitle; //Includes .tif
dotIndex = indexOf(name, ".");
title = substring(name, 0, dotIndex); //name without the extension
mouseAndRegionNumber = substring(output,33); //Extracts the mouse and
region number, in this case delta_1. Will change if file structure changes
print(mouseAndRegionNumber);
newFileName = mouseAndRegionNumber + "_" + title + "_";
print(newFileName);
firstPlaneName = output + "/" + title + "/";
print(firstPlaneName);
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName");
run("Close All");
}
input = "/C:/.../Raw/"; //Here i've omitted some directories with a ... just
for the purpose of posting this question
output = "/C:/.../delta_1";
list = getFileList(input);
for (i = 0; i < list.length; i++){
setBatchMode(false);
prepareForMatlab(input, output, list[i]);
}
I know this code is probably not the most elegant way to do this (accepting
suggestions on that as well!) but the problem I'm having now is that in the
line
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName")
newFileName is not treated as a variable with a string value but rather as a
string itself (so all the files end up having the word newFileName in them).
How do I fix this?
This code also has another weird problem where the images open one by one
and allow the user to move the sliders to see different z planes and then
adjust brightness/contrast. This works fine unless you try to switch back
and forth between toggling z planes and toggling brightness. (Then the macro
gives and error "no image open") No idea why this would happen...it works
fine if you do one and then the other, just not if you switch back and
forth.
Any help would be greatly appreciated!!!
--
View this message in context: http://imagej.1557.x6.nabble.com/Writing-a-macro-to-save-image-stack-as-series-of-separate-files-tp5009571.html
Sent from the ImageJ mailing list archive at Nabble.com.
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Kenton Arkill
2014-09-11 07:05:51 UTC
Permalink
Hi Janelle
If I understand you correctly I find the
*File.separator *macro function preferable to +"/"+ in building strings
(but if it ain't broke don't fix it!)

Kenton
Post by Volko Straub
In order for newFileName to be recognised as a variable inside this
string, you need to write it as
run("Image Sequence... ", "format=TIFF name="+newFileName+" start=0 digits=3
save="+firstPlaneName)
Note the "+ before the variable and the +" after the variable - this will
insert the content of your variable into the string.
I assume firstPlaneName at the end of that command is also a varibale. So,
you will have to add "+ before that variable name as well. You don't need a
+" at the end as there is no more text to be added to that string.
Below is an extract from the ImageJ Macro documentation that gives more details
The input fields of a dialog can be set to the contents of a macro
variables by using string concatentation (http://rsb.info.nih.gov/ij/
noise = 50; output = "Point Selection"; run("Find Maxima...",
"noise="+noise+" output=["+output+"] light"); With ImageJ 1.43 and later,
there is an easier method that only requires adding "&" to the variable
name in the options string: noise = 50; output = "Point Selection";
run("Find Maxima...", "noise=&noise output=&output light"); String
variables passed using this method do not have to be enclosed in brackets.
For more examples, see theArgumentPassingDemo <http://rsb.info.nih.gov/ij/
macros/ArgumentPassingDemo.txt>macro.
I hope this helps,
Volko
Post by jlw
Hi, I'm trying to write a macro to open image stacks, manually look at them
and adjust brightness/contrast, then save as a series of separate files,
each in its own folder. The way the files are named is not flexible because
they have to be named in a certain way to feed into another Matlab program
function prepareForMatlab(input, output, filename) {
setBatchMode(false);
open(input + filename);
run("Brightness/Contrast...");
waitForUser('Adjust brightness and contrast');
run("Close"); //The filename is automatically extracted but we
have to
modify this to get it without the extension
name = getTitle; //Includes .tif
dotIndex = indexOf(name, ".");
title = substring(name, 0, dotIndex); //name without the extension
mouseAndRegionNumber = substring(output,33); //Extracts the mouse and
region number, in this case delta_1. Will change if file structure changes
print(mouseAndRegionNumber);
newFileName = mouseAndRegionNumber + "_" + title + "_";
print(newFileName);
firstPlaneName = output + "/" + title + "/";
print(firstPlaneName);
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName");
run("Close All");
}
input = "/C:/.../Raw/"; //Here i've omitted some directories with a ... just
for the purpose of posting this question
output = "/C:/.../delta_1";
list = getFileList(input);
for (i = 0; i < list.length; i++){
setBatchMode(false);
prepareForMatlab(input, output, list[i]);
}
I know this code is probably not the most elegant way to do this (accepting
suggestions on that as well!) but the problem I'm having now is that in the
line
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3
save=firstPlaneName")
newFileName is not treated as a variable with a string value but rather as a
string itself (so all the files end up having the word newFileName in them).
How do I fix this?
This code also has another weird problem where the images open one by one
and allow the user to move the sliders to see different z planes and then
adjust brightness/contrast. This works fine unless you try to switch back
and forth between toggling z planes and toggling brightness. (Then the macro
gives and error "no image open") No idea why this would happen...it works
fine if you do one and then the other, just not if you switch back and
forth.
Any help would be greatly appreciated!!!
--
View this message in context: http://imagej.1557.x6.nabble.
com/Writing-a-macro-to-save-image-stack-as-series-of-
separate-files-tp5009571.html
Sent from the ImageJ mailing list archive at Nabble.com.
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
jlw
2014-09-12 03:04:44 UTC
Permalink
Great, that was just what I needed. Thanks!



--
View this message in context: http://imagej.1557.x6.nabble.com/Writing-a-macro-to-save-image-stack-as-series-of-separate-files-tp5009571p5009597.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Loading...