Adapt duplicate's name in a macro

Hello,
My programing skills are very poor and that’s why I need your help.
I want to do the same process to many many images and I thought of programing it (well, record it and then add/remove some things). I want to take an image, specify a region, duplicate that region, change the name adding a “_centered” (instead of a "_1) before the .tif and saving it. This is what I’ve got:

selectWindow("CurrentName.tif");
run("Specify...", "width=540 height=540 x=50 y=15");
run("Duplicate...", "title=CurrentName_centered.tif");
saveAs("Tiff", "C:\\Users\\Admin\\Desktop\\MyFolder\\CurrentName_centered.tif");

I have seen that there’s a similar topic https://forum.image.sc/t/using-duplicates-in-imagej-macros/992 but they start discussing and I lose track.
I guess I have to use “getImageID()”, but as I said, I am not very skilled.
I would be very grateful if you could help me.
Have a nice day :smiley:

You can do this as follows:

currentImage = getTitle();
dir = getDirectory("image");
run("Specify...", "width=540 height=540 x=50 y=15");
run("Duplicate...", "title="+currentImage +"_centered.tif");
duplicateImage = getTitle();
saveAs("Tiff", dir + duplicateImage);
2 Likes

First of all: thank you so much!
But can I bother you a bit more? When I do this, the image’s name is “Something.tif_centered.tif”. Is it possible to avoid the first .tif? With other kind of files happens the same, for example "Something.png_centered.tif.
But I am happy already, so thank you very much :smiley: :smiley: :smiley:

@Diego_Hens

You’ll want to check out the Built-In Macro Functions page to find some functions dealing with strings… If you print(currentImage);… I am assuming you get the string Something.tif.

So you can just get a substring of that string like…

currentImage = getTitle();
print(currentImage);
currentImageLength = lengthOf(currentImage);
currentImage = substring(currentImage, 0, currentImageLength-4); // the -4 gets rid of the .tif
print(currentImage);

The print() statements are just there as a check for you to see that the string is being changed…

Hope this helps!

2 Likes

Thank you so much! I added this to the code above and it works perfectly, as I wished for.
Thank you all so much :heart:

1 Like