Login  Register

Re: batch conversion of a folder

Posted by ctrueden on Mar 13, 2008; 2:54pm
URL: http://imagej.273.s1.nabble.com/batch-conversion-of-a-folder-tp3696887p3696892.html

Hi Younes,

You do not need the batch converter plugin to use the macro I sent. My guess
is that some of the files in the subfolders are in a format that ImageJ does
not recognize. You could either move them, or change the macro to ignore
files unless they end with a particular extension.

Below is a version that only converts files originally in TIFF format. I
noticed you also said you want to convert from 12-bit to 8-bit, so I put an
extra line in to do that as well.

You can add more statements in the same place, immediately following the
"while (nImages > 0)" command. You can easily discover what a statement to
perform a particular action looks like by using the Macro Recorder (Plugins
> Macros > Record) feature. Just perform an action using the menus, then
copy and paste the resulting command from the macro recorder window into
your macro.

------
// Recursively converts files to TIFF starting in a user-specified directory

requires("1.32f");
dir = getDirectory("Choose a Directory ");
count = 1;
prefix = "converted-";
listFiles(dir);

function listFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/"))
            listFiles(""+dir+list[i]);
        else if (endsWith(toLowerCase(list[i]), ".tif") ||
            endsWith(toLowerCase(list[i]), ".tiff"))
        {
            print((count++) + ": " + dir + list[i]);
            open(dir + list[i]);
            // convert all open images to TIFF
            c = 1;
            while (nImages > 0) {
                run("8-bit");
                // place additional commands here
                saveAs("Tiff", dir + prefix + list[i] + "-" + c + ".tif");
                close();
                c++;
            }
        }
    }
}
------

-Curtis

On Thu, Mar 13, 2008 at 8:33 AM, Younes Leysi Derilou <
[hidden email]> wrote:

> Dear Curtis,
>
> Thank you for your time to write the macro. i saved your text as
> "directory_convert_tiff.txt" in ij/macros folder and then run it. it
> asked me again to choose a directory which i selected the main folder.
> but then it showed an error like this "File is not in tiff, jpeg,
> giff, bmp .... format, a plugin for this format is not installed, or
> it was not found.". I have the batch converter plugin ( saved in
> ij/plugins) which works properly. I had also put the plugin into that
> main folder, but the same error came up. all of my images (in the
> subfloders) are in 12-bit tiff format and need to be converted to
> 8-bit tiff. is there something else i am missing to joint this plugin
> and macro together?
>
> thanks a lot for your kind collaboration,
>
> Best regards,
> Younes
>
> On Wed, Mar 12, 2008 at 4:40 PM, Curtis Rueden <[hidden email]> wrote:
> > Hi Younes,
> >
> > You need to modify the macro to perform the tasks you want. To get you
> > started, here is a version that recursively converts files to TIFF
> format:
> >
> > ------
> > // Recursively converts files to TIFF starting in a user-specified
> directory
> >
> > requires("1.32f");
> > dir = getDirectory("Choose a Directory ");
> > count = 1;
> > prefix = "converted-";
> > listFiles(dir);
> >
> > function listFiles(dir) {
> >    list = getFileList(dir);
> >    for (i=0; i<list.length; i++) {
> >        if (endsWith(list[i], "/"))
> >            listFiles(""+dir+list[i]);
> >        else {
> >            print((count++) + ": " + dir + list[i]);
> >            open(dir + list[i]);
> >            // convert all open images to TIFF
> >            c = 1;
> >            while (nImages > 0) {
> >                saveAs("Tiff", dir + prefix + list[i] + "-" + c +
> ".tif");
> >                close();
> >                c++;
> >            }
> >        }
> >    }
> > }
> > ------
> >
> > HTH,
> > Curtis
> >
> > On Wed, Mar 12, 2008 at 2:18 PM, Younes Leysi Derilou <
> >
> > [hidden email]> wrote:
> >
> > > Hello Curtis,
> > >
> > > Thank you for the link. i saved that file as "directory_call.txt" in
> > > "imageJ/macros/" folder. i run the macro first through
> > > "Menu/plugins/macro/run", which asked me to select the directory, i
> > > chose one (for example F:\test_conversion\day5c which had 49
> > > subfolders), then i run the batch converter plugin. that plugin just
> > > converted the images inside the subfolder that i had chosen, nothing
> > > else. i did not see any benefits of running that macro. i have also
> > > tried to run macro after that plugin, but nothing happened. is there
> > > other procedure that i should follow to joint the macro and plugin
> > > together?
> > >
> > > Thanks and Regards,
> > > Younes
> > >
> > > On Wed, Mar 12, 2008 at 2:06 PM, Curtis Rueden <[hidden email]>
> wrote:
> > > > Hi Younes,
> > > >
> > > > Take a look at the "list files recursively" macro:
> > > > http://rsb.info.nih.gov/ij/macros/ListFilesRecursively.txt
> > > >
> > > > -Curtis
> > > >
> > > >
> > > > On Wed, Mar 12, 2008 at 12:43 PM, Younes Leysi Derilou <
> > > > [hidden email]> wrote:
> > > >
> > > > > Hello ImageJ Lovers,
> > > > >
> > > > > I would like to convert 12-bit tiff images to 8-bit tiff images in
> a
> > > > > main folder which has already several sub-folders. the sub-folders
> > > > > have thousands of images which can be easily converted one-by-one,
> > > > > using the "batch converter" plugin  from the following link. the
> > > > > problem is that I have to use this plugin for all sub-folders one
> by
> > > > > one and the number of the sub-folders are too much. I am wondering
> if
> > > > > there is a way to run this plugin for the main folder; i.e. ask a
> > > > > macro to do the conversion job for all other sub-folders. any
> > > > > suggestions are highly appreciated.
> > > > > http://rsb.info.nih.gov/ij/plugins/batch-converter.html
> > > > >
> > > > > Truly yours,
> > > > > Younes
> > > > > --
> > > > > Younes Leysi
> > > > > "Life is a song, sing it."
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > Younes Leysi
> > > "Life is a song, sing it."
> > >
> >
>
>
>
> --
> Younes Leysi
> "Life is a song, sing it."
>