Batch stack to images, with renaming

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

Batch stack to images, with renaming

cellcyclist
I have many tiff files with multiple stacks in each tiff. I would like to split the stacks into individual tifs and save them in batch, with the file names of the new images labeled with the original image and info about the stack. For example:

Image 1.tiff
Image 2.tiff

converted to

Image 1-stack1.tiff
Image 1-stack2.tiff
Image 1-stack3.tiff
Image 2-stack1.tiff
Image2-stack2.tiff
Image2-stack3.tiff

I can sort of do what I want by opening all the original tiffs with stacks as Import->Image Sequence, then Save As->Image Sequence, but that renames all the files. I've tried using slice labels as file name when saving the image process, but that requires me going through and setting up each splice label, which defeats the whole point of batch.

Any thoughts or advice?
Reply | Threaded
Open this post in threaded view
|

Re: Batch stack to images, with renaming

Krs5
Dear jpmatson

If the number of images within each individual stack is the same the macro code below might work. Copy the code and past it into a macro editor (File > New > Text Window) and hit run.
If not, you might be able to optimize the code for your use.

macro split_stack{
dir1 = getDirectory("Choose source directory");
list = getFileList(dir1);
dir2 = getDirectory("Choose destination directory");
a = 1;
Dialog.create("Input");
 Dialog.addNumber("Number of images per stack",a);
Dialog.show();
a = Dialog.getNumber();
setBatchMode(true);
for (i=0; i<list.length; i++) {
 open(dir1+list[i]);
 getDimensions(width, height, channels, slices, frames);
 counter=1;
 title = File.nameWithoutExtension;
 for (j=1; j< slices;j+=a){
  k = j+a-1;
  run("Make Substack...", "channels=1-"+channels+" slices="+j+"-"+k);
  saveAs("tif", dir2+title+"-stack"+counter+".tiff");
  close();
  counter+=1;
 }
 close();
}
showMessage("Macro is finished");
}



Best wishes and Merry Christmas

Kees

Dr Ir K.R. Straatman
Senior Experimental Officer
Advanced Imaging Facility
Centre for Core Biotechnology Services
University of Leicester
http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/lite/aif
________________________________________
From: ImageJ Interest Group [[hidden email]] on behalf of cellcyclist [[hidden email]]
Sent: 20 December 2014 04:30
To: [hidden email]
Subject: Batch stack to images, with renaming

I have many tiff files with multiple stacks in each tiff. I would like to
split the stacks into individual tifs and save them in batch, with the file
names of the new images labeled with the original image and info about the
stack. For example:

Image 1.tiff
Image 2.tiff

converted to

Image 1-stack1.tiff
Image 1-stack2.tiff
Image 1-stack3.tiff
Image 2-stack1.tiff
Image2-stack2.tiff
Image2-stack3.tiff

I can sort of do what I want by opening all the original tiffs with stacks
as Import->Image Sequence, then Save As->Image Sequence, but that renames
all the files. I've tried using slice labels as file name when saving the
image process, but that requires me going through and setting up each splice
label, which defeats the whole point of batch.

Any thoughts or advice?



--
View this message in context: http://imagej.1557.x6.nabble.com/Batch-stack-to-images-with-renaming-tp5011012.html
Sent from the ImageJ mailing list archive at 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: Batch stack to images, with renaming

cellcyclist
Thanks Kees!

This is almost perfect. But if I have 3 stacks in the original tiff it only generates the first 2 stacks separately, and the 3rd stack is missing. Is there a way to specify the number of output substacks so that it makes 3 new stacks? (I'm new to ImageJ don't really know much about macros)

Thanks so much

Merry Christmas!
Reply | Threaded
Open this post in threaded view
|

Re: Batch stack to images, with renaming

Krs5
Not sure what is happening. I did some tests with the samples confocal-series and t1-head and used as number of stacks 5 and 43 resp. and get the expected number of files, 5 and 3. You should have no images open when you run the macro.

The macro asks you for the number of images per stack, this is the number of images in the new to creating stacks. The macro checks how many images (slices) there are in the original stack via getDimensions(width, height, channels, slices, frames); and if all is correct this should result in three new images as [images per stack]/slices = 3.

Are the stacks that are created correct and there is no error message?

How large are your data sets? Is it possible to upload one somewhere so I can download it?

BW

Kees
________________________________________
From: ImageJ Interest Group [[hidden email]] on behalf of cellcyclist [[hidden email]]
Sent: 23 December 2014 20:03
To: [hidden email]
Subject: Re: Batch stack to images, with renaming

Thanks Kees!

This is almost perfect. But if I have 3 stacks in the original tiff it only
generates the first 2 stacks separately, and the 3rd stack is missing. Is
there a way to specify the number of output substacks so that it makes 3 new
stacks? (I'm new to ImageJ don't really know much about macros)

Thanks so much

Merry Christmas!



--
View this message in context: http://imagej.1557.x6.nabble.com/Batch-stack-to-images-with-renaming-tp5011012p5011041.html
Sent from the ImageJ mailing list archive at 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: Batch stack to images, with renaming

cellcyclist
The files are at the link below, three stacks in each tiff, one color channel each, 34 tiffs total (though I may be doing more)
I've tried it with only one file and I still only get two stacks (blue and magenta, but no green channel)

https://www.dropbox.com/sh/x0zha3s47ybuyes/AAAW_RudM_CxCxWe5P4XxWrEa?dl=0


Thanks for your Help :)
Reply | Threaded
Open this post in threaded view
|

Re: Batch stack to images, with renaming

Krs5
Just to be sure, these 34 files should give in total 3 files, one for each colour, or you just want to split each file in 3 colours so you get in total 102 files? And these 34 files are your original files or are they one large file containing all the images? Or something else as I am not sure where the stack in the results file name refers to as it seems to be channels and not stacks?

Kees


________________________________________
From: ImageJ Interest Group [[hidden email]] on behalf of cellcyclist [[hidden email]]
Sent: 24 December 2014 22:39
To: [hidden email]
Subject: Re: Batch stack to images, with renaming

The files are at the link below, three stacks in each tiff, one color channel
each, 34 tiffs total (though I may be doing more)
I've tried it with only one file and I still only get two stacks (blue and
magenta, but no green channel)

https://www.dropbox.com/sh/x0zha3s47ybuyes/AAAW_RudM_CxCxWe5P4XxWrEa?dl=0


Thanks for your Help :)



--
View this message in context: http://imagej.1557.x6.nabble.com/Batch-stack-to-images-with-renaming-tp5011012p5011046.html
Sent from the ImageJ mailing list archive at 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: Batch stack to images, with renaming

cellcyclist
The 34 files should give one file for each color per image, 102 files at the end. The 34 files are the original files, I would like to be able to take large numbers of files and separate out them into the separate colors.

sorry about the stack/channel mix up, I don't really know what I am doing :)

Thanks so much for your time I really appreciate it
Reply | Threaded
Open this post in threaded view
|

Re: Batch stack to images, with renaming

Krs5
OK I understand what you try to do. See if the code below does what you want.

macro split_color{
 dir1 = getDirectory("Choose source directory");
 list = getFileList(dir1);
 dir2 = getDirectory("Choose destination directory");
 setBatchMode(true);
 for (i=0; i<list.length; i++) {
  open(dir1+list[i]);
  title1=File.nameWithoutExtension;
  for (j=1; j<=3; j++){
   run("Make Substack...", "  slices="+j);
   run("8-bit");
   if (j==1) c = "blue";
   else if(j==2) c = "purple";
   else c = "green";
   saveAs("tif", dir2+title1+"-"+c+".tif");
   wait(100);
   close();
  }
  close();
 }
 showMessage("Macro is finished");
}


Kees
________________________________________
From: ImageJ Interest Group [[hidden email]] on behalf of cellcyclist [[hidden email]]
Sent: 28 December 2014 04:50
To: [hidden email]
Subject: Re: Batch stack to images, with renaming

The 34 files should give one file for each color per image, 102 files at the
end. The 34 files are the original files, I would like to be able to take
large numbers of files and separate out them into the separate colors.

sorry about the stack/channel mix up, I don't really know what I am doing :)

Thanks so much for your time I really appreciate it



--
View this message in context: http://imagej.1557.x6.nabble.com/Batch-stack-to-images-with-renaming-tp5011012p5011053.html
Sent from the ImageJ mailing list archive at 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: Batch stack to images, with renaming

cellcyclist
That is exactly what I need, thank you for your time.