Saving an image sequence as separate images in a macro


#1

Hi All,

I am stumped and I am hoping for some assistance. I am currently using ImageJ 1.51. I have a lot of math to do on a lot of images that are in sequences. I need to do the math and then save the sequences as individual images. Because of the number of separate sequences, I wrote a macro to do this.

The mathematics in the macro are correct (I checked them by hand and it is all correct), however, I cannot seem to save the sequences as individual images. I used the run(“Image Sequence …”) command produced by the macro recorder.

After several iterations, I used the recorder to provide the commands to simply open a sequence and save it as separate images in another folder. It will not mean anything (pathwise), but here is the exact commands provided by the recorder:

run(“Image Sequence…”, “open=C:\Users\PSP_2\Desktop\20inchMach6\Run003\Ratio\Test7020_Run0003_Seq301_Image000.rat sort”);
run("Image Sequence… ", “format=FITS use save=C:\Users\PSP_2\Desktop\20inchMach6\Run003\Calibrated\Ratio0000.fits”);

(This is just a copy and paste, so no line breaks or anything like that)

If I copy and paste those exact lines in a macro, it fails. The sequence will open correctly, but the second line (to save the images) is simply ignored. There is no error provided. It just does not execute the command.

Any assistance would be greatly appreciated.

Thanks in advance,
Neal


#2

Oh…one other thing. I selected to use the slice names as the filenames.


#3

Good day Neal,

please have a look at the many example macros and guides to macro programming that can be accessed from here:
https://imagej.nih.gov/ij/developer/index.html

I’m sure you will find useful code that helps with your problem.

Have success

Herbie


#4

Hi Herbie,

I appreciate the links. However, this really does not explain why I can copy the EXACT commands from the macro recorder into a macro and it does not function properly. Again, these were the only commands.

I guess if it comes down to it, I will have to change the macro to pull each slice individually and save it.

Thanks,
Neal


#5

Neal,

ImageJ tells me that the images, if they are to be saved by

run("Image Sequence… ", “format=FITS use save=/Users/me/Downloads/ScreenShots”);

(I’m on a Mac) in a directory named “ScreenShots”, they need to be in a stack.
If they are in a stack, the images are save separately as desired.

HTH

Herbie


#6

Hi Herbie,

When I opened them with the run(“Image Sequence…”) command it put the images in a stack.

Thank you for your responses. I will try and figure out what is going on. I saw on some other posts where people were saving images separately from a stack. Their command had a slightly different format. Maybe I will try that.

It is just strange that using the exact command from the macro recorder is failing.

Neal


#7

This is how I do it with bioformats importer, maybe it can help (python code).

def BF_Importer():
    options = ImporterOptions()
    options.setId(filepath)
    options.setOpenAllSeries(True)
    options.setSplitChannels(False)
    imps = BF.openImagePlus(options)
    index = 0
	
    for imp in imps:
        IJ.saveAs(imp, "TIFF", savepath+str(index).zfill(3))
        index += index

#8

Thanks Sverre,

However, I have figured out the issue and I think it is a bug. A small bug…and a very annoying one.

It turns out the issue was in the last part of the second line. The macro recorder assigned the save part to save=C:\Users\PSP_2\Desktop\20inchMach6\Run003\Calibrated\Ratio0000.fits .

The issue is the .fits part. If that is truncated to three characters or less, the command works perfectly. Regardless of what you tell it to save as, it automatically appends the .fits extension.

So, if I wanted to name the file with a .tempK (for temperature in K), the command line would be ignored since the extension was more than three characters. However, if I wanted to name it .tpK, or .tK, or .fit, or .fts, then everything works as it should. The only thing is that the files will not have that extension. It will be .fits.

Long story short, I can do what I need to with the run("Image Sequence… ") command as long as the extension on the save= is three characters or less.

Thanks Herbie and Sverre for the responses and making me try and run this down.

Neal


#9

Neal,

I think you are on the wrong track:

The last part of the path for saving the images must be a directory, not a file name and of course no suffix.

I’ve mentioned that before. No bug here!

Regards

Herbie


#10

Except that the original command comes directly from the macro recorder. As I mentioned in the first post, if you simply copy the command from the recorder window and use that as a macro, it will fail. I am not sure if you call that a bug or not, but on the ImageJ macro reference site, it suggests using the macro recorder to determine syntax for the run(…) command. If you did that for saving a stack as individual images, it fails. I did not treat the save= part as an indicator of the directory (though it makes sense now) becaue the macro recorder actually supplies a filename.

Maybe it is not a bug, but it is very confusing at the least…


#11

Neal,

the line I’ve posted in my second contribution was recorded with the macro recorder and the last part of the path evidently is a directory.

Not sure what’s going wrong on your side.

Could you please create a folder named “test” on your desktop and run the following two macro lines with internet connection open

run(“MRI Stack (528K)”);
run("Image Sequence… ", “format=FITS use save=C:\Users\PSP_2\Desktop\test”);

and report what happens?

Regards

Herbie


#12

Thanks Herbie,

After I modified the macro (change the \ to \), it worked perfectly. Nice MRI images by the way. So I modified my call to not have a filename, and it worked perfectly too.

So, I tried another macro recording. On the dialog box for the Save As Image Sequence command, I kept the name blank (with the use slice as name checked). The next dialog has the file select box. However, there has to be a filename in that dialog box (you cannot just select a directory). This filename has .fits appended and this gets written to the macro recorder command, and that is causing the issue.

I forgot to mention it, but I am using a Windows 7 64-bit machine (should have mentioned that before)…

Basically, you are correct Herbie (as usual). The macro command works perfectly if the save= portion only has a directory. I was using the marco recorder supplied command and this had the filename attached.

Thanks for supplying the test macro and leading me through this. It is all working now.

I really appreciate it!!!

Neal


#13

Neal,

obviously I was aware that you are on windows because I wrote:

run("Image Sequence… ", “format=FITS use save=C:\Users\PSP_2\Desktop\test”);

.

After I modified the macro (change the \ to ) […]

Aha!

Happy coding

Herbie


#14

OOOPS…you are quite right…my bad.

And that was to change the single slash to a double slash in case it did not come out that way (like on my phone).