Macro to split images and save as title

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Macro to split images and save as title

bluebird-2
Hi all,

I'm new to ImageJ macros and am usually happy to use the macro recorder.
However, I can't seem to find a way to automate splitting images containing
multiple colour layers and then saving them as tiffs with the filename as
the title of the image. Maybe it's not doable as the files are .vsi format
(I use Olympus Viewer plugin to open them), but even just automating the
saving process after opening the images manually would be very helpful. It
seems I need to use a function like getTitle() and I found a couple of
threads that discuss something similar
(http://imagej.1557.x6.nabble.com/Using-getTitle-td3687039.html) but I can't
seem to get it to work.

Any help would be appreciated!



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

Krs5
Dear bluebird,

I don't know the Olympus viewer plugin but according to https://imagej.net/OlympusImageJPlugin it works in macros as well

so something like this might do the job:


dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        title = File.getNameWithoutExtension();
        saveAs("tiff", dir2+title+".tif");
        close();
    }
}

It will ask you were the files are you want to open and where you want to save the resulting split images. I assumes for the moment that you want to save your images as *.tif files. It also assumes that the folder with your Olympus files only contains these files and no other files.

Be aware if you copy the code above into the macro editor some of the " might be changed into some unreadable characters (at least for ImageJ). You can check this by using in the editor Edit > Zap Gremlins and add the correct ".

Hope it works

Kees
________________________________

Hi all,

I'm new to ImageJ macros and am usually happy to use the macro recorder.
However, I can't seem to find a way to automate splitting images containing
multiple colour layers and then saving them as tiffs with the filename as
the title of the image. Maybe it's not doable as the files are .vsi format
(I use Olympus Viewer plugin to open them), but even just automating the
saving process after opening the images manually would be very helpful. It
seems I need to use a function like getTitle() and I found a couple of
threads that discuss something similar
(https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2FUsing-getTitle-td3687039.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C1bf036ad2d3146009bb008d7fde02622%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C1%7C637257011478467046&amp;sdata=TZn0Nvdg2%2FHYJ9jnvzZpEtYu%2FTPbbDKaa0dVUi%2FVP2Y%3D&amp;reserved=0) but I can't
seem to get it to work.

Any help would be appreciated!



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C1bf036ad2d3146009bb008d7fde02622%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C1%7C637257011478467046&amp;sdata=8ZTsHBsJKoq%2FOVV6%2B4wg%2FqNfmL14%2B4t5UTfa%2BjPJaZA%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C1bf036ad2d3146009bb008d7fde02622%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C1%7C637257011478467046&amp;sdata=bPblPoCkf2dfpLdMZH8Tx3PxlJ09GRWCdHKapSYyON0%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

bluebird-2
Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

Krs5
Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C53b0809a43b241ee5a8508d8000946db%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637259387131383447&amp;sdata=Y16D%2F3g4ZBwAt3h2UuY6H1cK62UHvfTLiiKf06N6t7k%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C53b0809a43b241ee5a8508d8000946db%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637259387131383447&amp;sdata=GorCKFmH5IuAPZW07IkRAwM0hCZbLyiDiwm7XIEaBrU%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

CARL Philippe (LBP)
Dear Kees and Marc,
To answer your question, the generated picture names are in the form of "C1-imageName".
But reading your previous macro code, I found it quite strange on the first hand that you use the line:
title = File.getNameWithoutExtension(path);
on first hand.
Why don't you just use:
title = getTitle();
for each split picture?
I would even push it further by including directly the getTitle() inside the saveAs method.
And given that your path ends with ".tif", you can even simplify your code further by using the save method instead of the saveAs ending up by having:
dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        save(dir2+getTitle()+".tif");
        close();
    }
}
Take care,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "Straatman, Kees (Dr.)" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Mardi 26 Mai 2020 17:51:16
Objet: Re: Macro to split images and save as title

Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C53b0809a43b241ee5a8508d8000946db%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637259387131383447&amp;sdata=Y16D%2F3g4ZBwAt3h2UuY6H1cK62UHvfTLiiKf06N6t7k%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C53b0809a43b241ee5a8508d8000946db%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637259387131383447&amp;sdata=GorCKFmH5IuAPZW07IkRAwM0hCZbLyiDiwm7XIEaBrU%3D&amp;reserved=0

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

Krs5
Dear Carl

Is correct, but with getTitle() you get the ".nd2" extension included in the new name. I normally try to replace it with ".tif", but I agree, that is just cosmetic.

Kees

________________________________


Dear Kees and Marc,
To answer your question, the generated picture names are in the form of "C1-imageName".
But reading your previous macro code, I found it quite strange on the first hand that you use the line:
title = File.getNameWithoutExtension(path);
on first hand.
Why don't you just use:
title = getTitle();
for each split picture?
I would even push it further by including directly the getTitle() inside the saveAs method.
And given that your path ends with ".tif", you can even simplify your code further by using the save method instead of the saveAs ending up by having:
dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        save(dir2+getTitle()+".tif");
        close();
    }
}
Take care,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "Straatman, Kees (Dr.)" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Mardi 26 Mai 2020 17:51:16
Objet: Re: Macro to split images and save as title

Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=zh7NrS1xtmd2ky1Kj6lVEmfoc7yqYy1pLrxvj4VXrCw%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

CARL Philippe (LBP)
Dear Kees,
Then just use something like:
save(dir2+substring(getTitle(),0,lastIndexOf(getTitle(),"."))+".tif");
And you are all set...
My best regards,
Philippe

----- Le 26 Mai 20, à 18:38, Straatman, Kees (Dr.) [hidden email] a écrit :

Dear Carl

Is correct, but with getTitle() you get the ".nd2" extension included in the new name. I normally try to replace it with ".tif", but I agree, that is just cosmetic.

Kees

________________________________


Dear Kees and Marc,
To answer your question, the generated picture names are in the form of "C1-imageName".
But reading your previous macro code, I found it quite strange on the first hand that you use the line:
title = File.getNameWithoutExtension(path);
on first hand.
Why don't you just use:
title = getTitle();
for each split picture?
I would even push it further by including directly the getTitle() inside the saveAs method.
And given that your path ends with ".tif", you can even simplify your code further by using the save method instead of the saveAs ending up by having:
dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        save(dir2+getTitle()+".tif");
        close();
    }
}
Take care,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "Straatman, Kees (Dr.)" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Mardi 26 Mai 2020 17:51:16
Objet: Re: Macro to split images and save as title

Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=zh7NrS1xtmd2ky1Kj6lVEmfoc7yqYy1pLrxvj4VXrCw%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cf2cf5ebd82c0483e766b08d801913d77%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261070605209275&amp;sdata=BrMczMzkkzNitlp%2Byp%2BoNhYilfKQSwFplqtFM9ooow0%3D&amp;reserved=0

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

Cammer, Michael-3
In reply to this post by Krs5
I use these to strip extensions.

for instance,

rename(getTitleStripExtensions());


function getTitleStripExtensions() {
  t = getTitle();
  extensions = newArray(".tif", ".tiff", ".lif", ".lsm", ".czi", ".nd2", ".ND2");
  for(i=0; i<extensions.length; i++)
    t = replace(t, extensions[i], "");
  return t;
}  // end getTitleStripExtension()



function getTitleStripOneExtension() {
  t = getTitle();
  lastPeriod = lastIndexOf(t, ".")   // same as t.lastIndexOf(".");
  t = substring(t, 0, lastPeriod);
  return t;
}



Michael Cammer, Sr Research Scientist, DART Microscopy Laboratory

NYU Langone Health, 540 First Avenue, SK2 Microscopy Suite, New York, NY  10016

[hidden email]<mailto:[hidden email]>  http://nyulmc.org/micros  http://microscopynotes.com/

Voice direct only, no text or messages:  1-914-309-3270 and 1-646-501-0567



________________________________
From: Straatman, Kees (Dr.) <[hidden email]>
Sent: Tuesday, May 26, 2020 12:38:13 PM
To: [hidden email]
Subject: Re: Macro to split images and save as title

[EXTERNAL]

Dear Carl

Is correct, but with getTitle() you get the ".nd2" extension included in the new name. I normally try to replace it with ".tif", but I agree, that is just cosmetic.

Kees

________________________________


Dear Kees and Marc,
To answer your question, the generated picture names are in the form of "C1-imageName".
But reading your previous macro code, I found it quite strange on the first hand that you use the line:
title = File.getNameWithoutExtension(path);
on first hand.
Why don't you just use:
title = getTitle();
for each split picture?
I would even push it further by including directly the getTitle() inside the saveAs method.
And given that your path ends with ".tif", you can even simplify your code further by using the save method instead of the saveAs ending up by having:
dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        save(dir2+getTitle()+".tif");
        close();
    }
}
Take care,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "Straatman, Kees (Dr.)" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Mardi 26 Mai 2020 17:51:16
Objet: Re: Macro to split images and save as title

Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://urldefense.proofpoint.com/v2/url?u=https-3A__eur03.safelinks.protection.outlook.com_-3Furl-3Dhttp-253A-252F-252Fimagej.1557.x6.nabble.com-252F-26amp-3Bdata-3D02-257C01-257Ckrs5-2540leicester.ac.uk-257Cf2cf5ebd82c0483e766b08d801913d77-257Caebecd6a31d44b0195ce8274afe853d9-257C0-257C0-257C637261070605209275-26amp-3Bsdata-3Dzh7NrS1xtmd2ky1Kj6lVEmfoc7yqYy1pLrxvj4VXrCw-253D-26amp-3Breserved-3D0&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=hUBj2D5n6oKThx2L01qn8IORZb5f-ruLVXPmQ1zQNnM&m=Caw6ccLYV77PTAEvtzWhc3thv5L_7LvjpONnKZKTT88&s=OtYozwtGf7UVmHuHZ-DzlN3-MQ7kNCJXNRSYcTgSwTM&e=

--
ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=https-3A__eur03.safelinks.protection.outlook.com_-3Furl-3Dhttp-253A-252F-252Fimagej.nih.gov-252Fij-252Flist.html-26amp-3Bdata-3D02-257C01-257Ckrs5-2540leicester.ac.uk-257Cf2cf5ebd82c0483e766b08d801913d77-257Caebecd6a31d44b0195ce8274afe853d9-257C0-257C0-257C637261070605209275-26amp-3Bsdata-3DBrMczMzkkzNitlp-252Byp-252BoNhYilfKQSwFplqtFM9ooow0-253D-26amp-3Breserved-3D0&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=hUBj2D5n6oKThx2L01qn8IORZb5f-ruLVXPmQ1zQNnM&m=Caw6ccLYV77PTAEvtzWhc3thv5L_7LvjpONnKZKTT88&s=sCbF7cQDKnTrMe7AI9Ii0nE66bqIF3aRPVcs-ldBGzM&e=

--
ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=https-3A__eur03.safelinks.protection.outlook.com_-3Furl-3Dhttp-253A-252F-252Fimagej.nih.gov-252Fij-252Flist.html-26amp-3Bdata-3D02-257C01-257Ckrs5-2540leicester.ac.uk-257Cf2cf5ebd82c0483e766b08d801913d77-257Caebecd6a31d44b0195ce8274afe853d9-257C0-257C0-257C637261070605209275-26amp-3Bsdata-3DBrMczMzkkzNitlp-252Byp-252BoNhYilfKQSwFplqtFM9ooow0-253D-26amp-3Breserved-3D0&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=hUBj2D5n6oKThx2L01qn8IORZb5f-ruLVXPmQ1zQNnM&m=Caw6ccLYV77PTAEvtzWhc3thv5L_7LvjpONnKZKTT88&s=sCbF7cQDKnTrMe7AI9Ii0nE66bqIF3aRPVcs-ldBGzM&e=

--
ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=https-3A__eur03.safelinks.protection.outlook.com_-3Furl-3Dhttp-253A-252F-252Fimagej.nih.gov-252Fij-252Flist.html-26amp-3Bdata-3D02-257C01-257Ckrs5-2540leicester.ac.uk-257Cf2cf5ebd82c0483e766b08d801913d77-257Caebecd6a31d44b0195ce8274afe853d9-257C0-257C0-257C637261070605209275-26amp-3Bsdata-3DBrMczMzkkzNitlp-252Byp-252BoNhYilfKQSwFplqtFM9ooow0-253D-26amp-3Breserved-3D0&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=hUBj2D5n6oKThx2L01qn8IORZb5f-ruLVXPmQ1zQNnM&m=Caw6ccLYV77PTAEvtzWhc3thv5L_7LvjpONnKZKTT88&s=sCbF7cQDKnTrMe7AI9Ii0nE66bqIF3aRPVcs-ldBGzM&e=

--
ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=http-3A__imagej.nih.gov_ij_list.html&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=hUBj2D5n6oKThx2L01qn8IORZb5f-ruLVXPmQ1zQNnM&m=Caw6ccLYV77PTAEvtzWhc3thv5L_7LvjpONnKZKTT88&s=YFdmSZiyBlO2iMnLDlUD7nI5yqAk06c6ehIHA1isA24&e=

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

Krs5
In reply to this post by CARL Philippe (LBP)
Dear Philippe and Michael,

OK, both will work. Many thanks.

However, I am plying around a little and now notice that if I have a nd2 file and just safe it as a tif file the extension seems automatically being replaced. Was not aware of this (or maybe had forgotten...).

Best wishes

Kees



________________________________
Dear Kees,
Then just use something like:
save(dir2+substring(getTitle(),0,lastIndexOf(getTitle(),"."))+".tif");
And you are all set...
My best regards,
Philippe

----- Le 26 Mai 20, à 18:38, Straatman, Kees (Dr.) [hidden email] a écrit :

Dear Carl

Is correct, but with getTitle() you get the ".nd2" extension included in the new name. I normally try to replace it with ".tif", but I agree, that is just cosmetic.

Kees

________________________________


Dear Kees and Marc,
To answer your question, the generated picture names are in the form of "C1-imageName".
But reading your previous macro code, I found it quite strange on the first hand that you use the line:
title = File.getNameWithoutExtension(path);
on first hand.
Why don't you just use:
title = getTitle();
for each split picture?
I would even push it further by including directly the getTitle() inside the saveAs method.
And given that your path ends with ".tif", you can even simplify your code further by using the save method instead of the saveAs ending up by having:
dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
        save(dir2+getTitle()+".tif");
        close();
    }
}
Take care,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "Straatman, Kees (Dr.)" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Mardi 26 Mai 2020 17:51:16
Objet: Re: Macro to split images and save as title

Dear Marc,

OK, so ImageJ does not recognize the file name opened by the plugin. So we can use something like

title = File.getNameWithoutExtension(path);

But we still have to take care that each channel is saved correctly. If the channels are split is the naming like "C1-imageName" or "imageName (red)"?

Kees

________________________________


Deer Kees,

Thank you very much for helping! I copied the macro into the editor and
removed gremlins as you described and checked that the " look the same (it
doesn't appear to have been changed). When running the macro I get asked
first source then destination directory as expected. It then asks me to
select a file (but only a single one, if I run it as batch would it be able
to take a whole directory of files?). However, after opening the file and
splitting it I get the following error message:

Number or numeric function expected in line 10
title = File.getNameWithoutExtension();

And it stops there unable to save.

Marc



--
Sent from: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.1557.x6.nabble.com%2F&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196528799&amp;sdata=e3Ri5OZQ41Q9t%2F9P8JgPN%2BILB8zpX93sdwNsEhpSkt8%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196528799&amp;sdata=r0wwbfd%2BFwrhfv8l3%2FlY95yA%2Ft16Q4g1xA55YXe2JLk%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196528799&amp;sdata=r0wwbfd%2BFwrhfv8l3%2FlY95yA%2Ft16Q4g1xA55YXe2JLk%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196528799&amp;sdata=r0wwbfd%2BFwrhfv8l3%2FlY95yA%2Ft16Q4g1xA55YXe2JLk%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196528799&amp;sdata=r0wwbfd%2BFwrhfv8l3%2FlY95yA%2Ft16Q4g1xA55YXe2JLk%3D&amp;reserved=0

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7Cd8833901b1ba4eeddc7908d801946801%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C0%7C637261084196538799&amp;sdata=irpae%2BqDlbtxRUJBpCe6WbS8vpa5QS43LZA8srJiicY%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

bluebird-2
In reply to this post by CARL Philippe (LBP)
Dear Kees and Philippe

Please accept my apology in replying so late, I got drowned out by another
project. Thank you both very much for your input. I tried out the
suggestions you both made, but the macro seems to get stuck at making the
Viewer open the files. First I'm asked to select source and destination
directory, which works. Immediately following that is a request from the
Olympus Viewer dialogue to open a file (can only select one). If clicking
canncel, this is followed by the error:

There are no images open in line 7
getDimensions(width, height, channels, slices, frames);

When choosing a file (can only click one), the images are split and the
following error appears:

File saving error (IOException)
"C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif (the
filename, direc"
C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif

Possibly the error message's first line is supposed to be longer, but it
ends like that.

Thank you in advance for any further help!

Best,
Marc



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

CARL Philippe (LBP)
Dear Marc,
I'm just before going to sleep and thus will read your mail more deeply and seriously tomorrow morning in order to better understand the description of your first issue (i.e. about file importing).
Nevertheless, your second issue, i.e. the one about the error message you are getting because you are trying to save the following file:
"C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif"
is trivial and simply due to the fact that you have ':' characters within your filenames which aren't allowed.
Thus in order to fix this, just use the replace(string, old, new) method:
https://imagej.nih.gov/ij/developer/macro/functions.html#replace
in order to replace all your ':' characters by other ones, maybe '_'.
Thus simply replace the following line code I had recommended you some time ago:
save(dir2+substring(getTitle(),0,lastIndexOf(getTitle(),"."))+".tif");
by
save(dir2+replace(substring(getTitle(),0,lastIndexOf(getTitle(),".")),":","_")+".tif");
And you should be all set...
My best regards,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "bluebird" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Jeudi 11 Juin 2020 20:40:21
Objet: Re: Macro to split images and save as title

Dear Kees and Philippe

Please accept my apology in replying so late, I got drowned out by another
project. Thank you both very much for your input. I tried out the
suggestions you both made, but the macro seems to get stuck at making the
Viewer open the files. First I'm asked to select source and destination
directory, which works. Immediately following that is a request from the
Olympus Viewer dialogue to open a file (can only select one). If clicking
canncel, this is followed by the error:

There are no images open in line 7
getDimensions(width, height, channels, slices, frames);

When choosing a file (can only click one), the images are split and the
following error appears:

File saving error (IOException)
"C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif (the
filename, direc"
C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif

Possibly the error message's first line is supposed to be longer, but it
ends like that.

Thank you in advance for any further help!

Best,
Marc



--
Sent from: http://imagej.1557.x6.nabble.com/

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

CARL Philippe (LBP)
Dear Marc,
For your issue about "correctly opening your files", I don't have the Olympus Viewer plugin installed on my machine, nor access to any native Olympus native files in order to try to reproduce it.
Nevertheless, could you please "open by hand" one of your file using this Olympus Viewer and having the "Plugins>Macros>Record..." launched with the option "Macro" chosen ?
What is then the displayed code you then get?
Is it something similar to:
    run("Viewer", "open=[path]");
Or rather something a "little bit more complicated" like:
    run("Viewer", "open=[path] chosen_file=a_number option=whatever ...");
Could you reply back with your obtained code?
My best regards,
Philippe

----- Mail original -----
De: "CARL Philippe, LBP" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Vendredi 12 Juin 2020 01:47:17
Objet: Re: Macro to split images and save as title

Dear Marc,
I'm just before going to sleep and thus will read your mail more deeply and seriously tomorrow morning in order to better understand the description of your first issue (i.e. about file importing).
Nevertheless, your second issue, i.e. the one about the error message you are getting because you are trying to save the following file:
"C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif"
is trivial and simply due to the fact that you have ':' characters within your filenames which aren't allowed.
Thus in order to fix this, just use the replace(string, old, new) method:
https://imagej.nih.gov/ij/developer/macro/functions.html#replace
in order to replace all your ':' characters by other ones, maybe '_'.
Thus simply replace the following line code I had recommended you some time ago:
save(dir2+substring(getTitle(),0,lastIndexOf(getTitle(),"."))+".tif");
by
save(dir2+replace(substring(getTitle(),0,lastIndexOf(getTitle(),".")),":","_")+".tif");
And you should be all set...
My best regards,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Mail original -----
De: "bluebird" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Jeudi 11 Juin 2020 20:40:21
Objet: Re: Macro to split images and save as title

Dear Kees and Philippe

Please accept my apology in replying so late, I got drowned out by another
project. Thank you both very much for your input. I tried out the
suggestions you both made, but the macro seems to get stuck at making the
Viewer open the files. First I'm asked to select source and destination
directory, which works. Immediately following that is a request from the
Olympus Viewer dialogue to open a file (can only select one). If clicking
canncel, this is followed by the error:

There are no images open in line 7
getDimensions(width, height, channels, slices, frames);

When choosing a file (can only click one), the images are split and the
following error appears:

File saving error (IOException)
"C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif (the
filename, direc"
C:\destinationfolder\C2process_1055 Group:1 Level:1 Area:1.tif

Possibly the error message's first line is supposed to be longer, but it
ends like that.

Thank you in advance for any further help!

Best,
Marc



--
Sent from: http://imagej.1557.x6.nabble.com/

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

bluebird-2
Dear Philippe,

Apologies once again for my delay. I really appreciate you helping me, I
will need to do this split+save operation thousands of times in the future
so it would be great to find a way not to have to do it manually.

To answer your first reply, I tried replacing the line that you mentioned to
save, now it says 'undefined error in line 11: saveAs("tiff",
dir2+title+".tif");

Maybe I put it in the wrong place? The entire macro below.

Meanwhile, because I keep getting asked by the Olympus plugin to open each
file individually when running this macro it seems it's not the ideal way.
However, it's easy for me to open all the .vsi files at once (they are the
files that need to be split into channels and saved) in ImageJ using another
Olympus plugin (called 'Drag Drop'). If I can just run a macro that targets
every open .vsi file one at a time, splits it, saves the results relative to
their title names (typically something like C1-process_1352.vsi) and then
moves onto the next open vsi file it would be perfect. Then I can just open
a whole folder at once, run the macro and have it all autosave.

To answer your question about what the command to call Olympus Viewer is, it
simply calls:

run("Viewer");

In the macro recorder.

Thank you once again!

Best,
Marc

dir1 = getDirectory("Select folder with images");
list = getFileList(dir1);
dir2 = getDirectory("Select folder to save the results");
for (i=0; i<list.length; i++){
    path = dir1 + list[i];
    run("Viewer", "open=[path]");
    getDimensions(width, height, channels, slices, frames);
    run("Split Channels");
    for (j=0; j<channels;j++){
       
save(dir2+replace(substring(getTitle(),0,lastIndexOf(getTitle(),".")),":","_")+".tif");
        saveAs("tiff", dir2+title+".tif");
        close();
    }
}



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to split images and save as title

bluebird-2
Solved:

Philippe Carl made some further adjustments to the macro and it works
beautifully now. Posting it below. Thank you everyone for your kind help!

Best,
Marc

Macro for importing, splitting and saving .vsi files:

        dir1 = getDirectory("Select folder with images");
        list = getFileList(dir1);
        dir2 = getDirectory("Select folder to save the results");
        for (i = 0; i < list.length; i++)
        {
                if(endsWith(list[i], ".vsi") || endsWith(list[i], ".ets"))
                {
                        path = dir1 + list[i];
                        ext = substring(path, lastIndexOf(path, "."), lengthOf(path));
                        run("Bio-Formats Macro Extensions");
                        Ext.setId(path);
                        Ext.getSeriesCount(seriesCount);
                        print("Number of pictures:     " + seriesCount);
                        for(n = 1; n <= seriesCount; n++)
                        {
                                run("Bio-Formats Importer", "open=[" + path + "] autoscale
color_mode=Composite view=Hyperstack stack_order=XYCZT series_" + n);
                                if(endsWith(getTitle(), "macro image"))
                                {
                                        close();
                                }
                                else
                                {
                                        getDimensions(width, height, channels, slices, frames);
                                        run("Split Channels");
                                        for (j = 0; j < channels; j++)
                                        {
                                                outpath = dir2 + replace(replace(getTitle(), replace(dir1, "\\", "/"),
""), ext, "") + ".tif";
                                                print(outpath);
                                                save (outpath);
                                                close();
                                        }
                                }
                        }
                }
        }



--
Sent from: http://imagej.1557.x6.nabble.com/

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