Need help with IF function

macro
Tags: #<Tag:0x00007fd53f7e12f8>

#1

My goal is to save 2 pictures from the active picture. Specifically, if the active picture is “MCQ.jpg”, I want to save the 1st one in the selected directory as “MCQ1.jpg” and the 2nd one as “MCQ2.jpg” Yet, my code keeps giving me “MCQ.jpg” in the selected directory.

My code so far:

dir = getDirectory("select the directory");
name = getTitle;
path1 = dir+name+"1"; 
path2 = dir+name+"2"; 
if (!File.exists(path1)) {
	saveAs("jpeg", path1);
}
else {
	saveAs("jpeg", path2);
}

I guess the if-else part has some error. Could someone help me out? Thank you very much.


#2

Dear @Fijipuff
first of all, I am not understanding why you need an IF to do that.

This program will save or as path1 or as path2 depending on the fact that path1 is already a file.
To be more clear, it will save to path2 only if you have already saved your picture in path1: so if you launch two times the program it will save the first time to path1 the second time to path2.
To save directly to both paths you have to remove your if.

Anyway, the IF part is correct the problem is above… following a way to create two paths as you requested.

open("https://i.ytimg.com/vi/E4X8BhlsxVk/maxresdefault.jpg");
dir = getDirectory("select the directory");
name = getTitle;

/* this is not correct ;) you are creating that way two file called  name.jpg1 and name.jpg2 
path1 = dir+name+"1";
path2 = dir+name+"2";
IJ.log(path1); ->  /Users/emartini/Desktop/test_save/maxresdefault.jpg1
IJ.log(path2); -> /Users/emartini/Desktop/test_save/maxresdefault.jpg2
*/

// you could do that

name_new1 = replace(name, ".jpg", "1.jpg");
name_new2 = replace(name, ".jpg", "2.jpg");

path1_new = dir+name_new1;
path2_new = dir+name_new2;

if (!File.exists(path1_new)) {
saveAs("jpeg", path1_new);
}
else {
saveAs("jpeg", path2_new);
}

hoping to be helpful,
have a nice day
Emanuele Martini


#3

Thanks Emanuele! Sorry I confused you. I didn’t know that after running the code, ImageJ would close the original picture and open the new picture instead. Is there any way for me to stay on the original picture? Like after saving the “MCQ1.jpg”, I still want the “MCQ.jpg” to be active since I need to do some other measurements and save that new picture as “MCQ2.jpg” That is why I used the IF function.


#4

Dear @Fijipuff,
ok I think you need something like that

dir = getDirectory("select the directory");
open("https://i.ytimg.com/vi/E4X8BhlsxVk/maxresdefault.jpg");
// get the original name and id, we will need them
original_name = getTitle;
original_id = getImageID()

// FIRST OPERATION
// convert to 8bit
run("8-bit");
// replace the name using the original name
name_new1 = replace(original_name, ".jpg", "1.jpg");
path1_new = dir+name_new1;
saveAs("jpeg", path1_new);

// select the image (this is necessary if you have more than one image opened, and you want to be sure to work on the corret image)
selectImage(original_id);
rename(original_name); //to keep the same name, you can also avoid it

// SECOND OPERATION
// invert gray scale
run("Invert");

name_new2 = replace(original_name, ".jpg", "2.jpg");
path2_new = dir+name_new2;
saveAs("jpeg", path2_new);
selectImage(original_id);
rename(original_name); //to keep the same name, you can also avoid it

This will do:

  1. a first operation (i.e. convert to 8bit) and save this modification as original_name1.jpg
  2. then do a second operation (i.e. invert) and save this modification as original_name2.jpg

Please take note that the original_name2.jpg so it will be 8bit AND inverted.

If your goal is to save original_name1.jpg in 8 bit
and original_name2.jpg just inverted (not also converted to 8 bit)
you will need to duplicate your image and work with a duplicate of your image.

Have a nice day,
Emanuele Martini


#5

just an add, it doesn’t close the image it just renames it with the saving name.
It’s a very annoying behaviour in my opinion, but it does that…