saving images from a stack

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

saving images from a stack

octavius
Hello everyone,

I have a stack of 1000 frames and I would like to convert those to images and save them in their proper order.
So something like this (this is simply for n=3) so obviously the last image in ImageJ pops up first and so I save in descending order. And each saved image is then closed:

run("Stack to Images");
saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame3.tif");
close();
saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame2.tif");
close();
saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame1.tif");
 etc........


Below is the script I wrote:


n=nSlices();
n=1000;
run("Stack to Images");

for (i=1; i<=n; i++)        
{
         a=i;
         showProgress(i, n);
         setSlice(i);  
         saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\"+frame_a+".tif");
         close();
}

close();


There is definitely a problem with this loop. Can anyone kindly shed some light and help me with it?

Thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

Michael Schmid
Hi Octavius,

yo probably want to have 'frame' as part of the String:

  saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame"+a+".tif");

For having nicer numbers '0001' to '1000', you could also try replacing 'a=i' (which would be  unnecessary anyhow):
  a = IJ.pad(i, 4);

Michael
________________________________________________________________
On May 24, 2012, at 20:34, octavius wrote:

> Hello everyone,
>
> I have a stack of 1000 frames and I would like to convert those to images
> and save them in their proper order.
> So something like this (this is simply for n=3) so obviously the last image
> in ImageJ pops up first and so I save in descending order. And each saved
> image is then closed:
>
> run("Stack to Images");
> saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame3.tif");
> close();
> saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame2.tif");
> close();
> saveAs("Tiff", "C:\\Users\\....\\....\\.....\\.....\\....\\frame1.tif");
> etc........
>
>
> Below is the script I wrote:
>
>
> n=nSlices();
> n=1000;
> run("Stack to Images");
>
> for (i=1; i<=n; i++)        
> {
>         a=i;
>         showProgress(i, n);
>         setSlice(i);  
>         saveAs("Tiff",
> "C:\\Users\\....\\....\\.....\\.....\\....\\"+frame_a+".tif");
>         close();
> }
>
> close();
>
>
> There is definitely a problem with this loop. Can anyone kindly shed some
> light and help me with it?
>
> Thanks in advance
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/saving-images-from-a-stack-tp4998795.html
> Sent from the ImageJ mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

ncarducci
In reply to this post by octavius
It would probably be easier if you didn't convert the entire stack to images, and instead copied each slice to a new image and saved those. I wrote up the code for it, in case my explanation wasn't clear.

I wrote it to work for a batch of images, so if that doesn't apply to you, then just use the processFiles function. I also have it saving to directory that the original image is stored in, with "Slice x" appended to the file name. For my test images, the dimensions were 1024x1024 and they were white, so change those if you want something different.


//Nicholas M. Carducci, 5/24/12

requires("1.33s");
dir = getDirectory("Choose a source directory ");
setBatchMode(true);
count = 0;
countFiles(dir);
n = 0;
processFiles(dir);

function countFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/"))
            countFiles(""+dir+list[i]);
        else
            count++;
    }
}

function processFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/"))
            processFiles(""+dir+list[i]);
        else {
            showProgress(n++, count);
            path = dir+list[i];
            processFile(path);
        }
    }

function processFile(path) {
    if (endsWith(path, ".tif")) {
        open(path);
        title=getTitle();
        len = lengthOf(title);
        lenPath = lengthOf(path);
        path = substring(path, 0, lenPath-len);
        for(i=1;i<=1000; i++){
          newTitle = substring(title, 0, len-4)+" Slice "+i;
          newPath = path+newTitle;
          run("Select All");
          setSlice(i);
          run("Copy");
          newImage("Untitled", "8-bit White", 1024, 1024, 1);
          run("Paste");
          saveAs("Tiff", newPath);
          close();
        }
      close();
    }
}

Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

octavius
In reply to this post by Michael Schmid
Thank you kindly Micheal!
Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

octavius
In reply to this post by ncarducci
Thank you kindly as well Ncarducci!
Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

daggaz
In reply to this post by ncarducci
the following changes will save your files under the original filenames, if you loaded a stack using import->image sequence.  im not sure if it will overwrite, I always unstack my images in a new directory to avoid losing my originals.  

function processFile(path) {
    if (endsWith(path, ".tif")) {
        open(path);
        title=getTitle();
        len = lengthOf(title);
        lenPath = lengthOf(path);
        path = substring(path, 0, lenPath-len);
        for(i=1;i<=1000; i++){
          newfile = getInfo("slice.label");
          newTitle = newfile;
          newPath = path+newTitle;
          run("Select All");
          setSlice(i);
          run("Copy");
          newImage("Untitled", "16-bit Black", 2048, 2048, 1);
          run("Paste");
          saveAs("Tiff", newPath);
          close();
        }
Reply | Threaded
Open this post in threaded view
|

Re: saving images from a stack

Krs5
Any reason you don't use:

path=getDirectory("image");

Kees


Dr Ir K.R. Straatman
Senior Experimental Officer
Centre for Core Biotechnology Services
College of Medicine, Biological Sciences and Psychology
University of Leicester

Fluorescence microscopy workshops 20-24 August: http://www.le.ac.uk/biochem/microscopy/workshop2012.html


-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of daggaz
Sent: 13 June 2012 23:45
To: [hidden email]
Subject: Re: saving images from a stack

the following changes will save your files under the original filenames, if
you loaded a stack using import->image sequence.  im not sure if it will
overwrite, I always unstack my images in a new directory to avoid losing my
originals.  

function processFile(path) {
    if (endsWith(path, ".tif")) {
        open(path);
        title=getTitle();
        len = lengthOf(title);
        lenPath = lengthOf(path);
        path = substring(path, 0, lenPath-len);
        for(i=1;i<=1000; i++){
          newfile = getInfo("slice.label");
          newTitle = newfile;
          newPath = path+newTitle;
          run("Select All");
          setSlice(i);
          run("Copy");
          newImage("Untitled", "16-bit Black", 2048, 2048, 1);
          run("Paste");
          saveAs("Tiff", newPath);
          close();
        }

--
View this message in context: http://imagej.1557.n6.nabble.com/saving-images-from-a-stack-tp4998795p4999052.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