Macro saving images after processing

Hello,

I’m a beginner and want to create a macro for a background substaction and saving the image.
Therfore I need to open the images manually to set the correct ROI. Then i want to start the Macro. My problem is that the saving doesn’t work.

Here is my actual Macro for imagej.

folder = “C:/Users/gi3076/Pictures/Test_Macro/ATP4_24h_DAPI_BS”
originalimage = getTitle();

run(“Measure”);
macro “Subtract Measured Background 1” {
if (selectionType==-1)
exit(“This macro requires an area selection”);
for (i=1; i<=nSlices; i++) {
setSlice(i);
getStatistics(area, mean);
run(“Select None”);
run(“Subtract…”, “value=”+mean);

  }

}

saveAs(“Tiff”, folder + originalimage);

maybe somebody can tell me what’s wrong?

I also read a lot about using “directory” codes, but i didn’t understand how to use them.

I would be greatful for some help.

Thanks a lot!

MM

HI @Michaela24 and welcome to the forum!

The quick answer is that your output path does not have a separator between the path C:/Users/gi3076/Pictures/Test_Macro/ATP4_24h_DAPI_BS and the filename (represented by originalimage).

You can hard code this by adding a slash or use the built in File.separator function (see here:Built-in Macro Functions)
Thus your save string would look like this:
saveAs("Tiff", folder+File.separator+originalimage);

Hope that helps!

1 Like

Hi @Michaela24 ,

I have a lecture series about this on youtube. Going through a directory for example is explained here:

Let us know if this helps!

Best,
Robert

thank you for your quick answer.
I tried both options but it still doesn’t work…

But I also found out that if i split the background substraction macro from the “saving” macro the saving works?
It’s confusing.

For simple scripts, you really don’t need to specify a macro block, you can just run the whole script:

folder = "C:/Users/gi3076/Pictures/Test_Macro/ATP4_24h_DAPI_BS"
originalimage = getTitle();

run("Measure");

if (selectionType==-1) { 
    exit("This macro requires an area selection");
}
for (i=1; i<=nSlices; i++) {
	setSlice(i);
	getStatistics(area, mean);
	run("Select None");
	run("Subtract…", "value="+mean);
}

saveAs("Tiff", folder+File.separator+originalimage);

But there are a couple of things to touch on:

  • If you could share an image, it would be much easier for people to help you troubleshoot your process.
  • It’s also unclear exactly what you’re trying to do. By my read the script above does the following:
    – takes an open image and pulls the title (so far so good)
    – Measures a selection if there is one and the whole image if not (is there a reason to do this?)
    – checks for any selection (not specifically an area selection) exit if no selection
    – loops slices and for each slice measure area and mean
    – clear selections
    – background subtract

One problem here is that you’re clearing the selection on the first slice, so after the area selection is measured on slice 1, you’re background subtracting, then on slice 2 getStatistics(area, mean); is measuring the whole image (there is no longer a selection).

If you could clarify whether you are looking to background subtract each image separately or all based on the measurement of the first slice, that would help.

I solved the problem. It was one bracket too much so the last part for saving the image couldn’t run.

but to answer your question, i used a macro from the internet to do this.
i use an open image and put a selection on the background (because of different images i have to do this manually)
then I start the macro

  • measure the selection, placed in on the background (so i can save the measured results, an know the values that were substracted for if necessary a troubleshooting)
  • it should only run if there is a selection (little bit like a control for me)
  • if there is a selection → this will be measured an the mean will be used to substract the background
  • after this saving

and i want to substract background for each image separately.

okay the point with the selection is not good maybe i should rewrite it.

Thanks :slight_smile: