Open an image, rotate it by set increments while saving each individual image

Hello,

I am fairly new to (Fiji Is Just) ImageJ. I know that ImageJ is able to take Python scripts (Jython).

I would like to try to write a script that will rotate an image incrementally over a set range and save the image at each increment. For example, rotate the image 90 degrees and save that image, then rotate another 90 degrees (now 180 from start) and save that image, etc.

I do not know how to open an image and call built-in functions that ImageJ has through a script. I do know that I can open an image by File / Open and rotate by Image / Transform / Rotate … , but I do not know how to call those features in a Jython script. This is mainly to save time on my part.

Any help and/or advice would be much appreciated!

All the best,

Ryan

1 Like

Hello,

As an update, I was able to finally get a script running:


import os
from ij import IJ

#--------------------------------------------------------------------

IJ.run(“Open…”)

i = 0
while (i <= 4):
print(i)
IJ.run(“Rotate…”, “angle=”+str(90) + " grid=0 interpolation=Bilinear enlarge")
IJ.run(“PNG…”)
i += 1


The IJ.run(“PNG…”) allows me to save the current image. However, this brings up a window where I can type the name, where I would like it to automatically be saved. For example, using the while loop holder “i”, save the newly rotated file as "filename_i.png).

Any help and/or advice would be appreciated!

All the best,

Ryan

1 Like

Hey @rdudschus ,

do you insist on using Jython/Python? I’m asking because ImageJ/Fijis macro recorder allows you pretty easily to record any sequence of menu clicks and executing them afterwards. It works pretty well when recording macro, but not so well for jython scripters. Here’s a video demonstrating the principle:

Furthermore, I have a small macro ready that does almost what you would like to do. It utilizes the graphics card to rotate an image repeatedly and makes a video out of the single rotated images:

In order to run that script, you need to activate the clij update site in Fiji as demonstrated here.

Alternatively, if you really would like to learn Jython in Fiji, I recommend this tutorial.

Let us know if that helps!

Cheers,
Robert

1 Like

Hi Robert,

Wow! I had no idea there was a recording macro. Thank you!

I do not insist on using Python at all. It is the only language that I know how to code it. Thank you for the script as well! I will definitely check it out!

The ultimate goal of this project is this …

Say the image that you open contains a diffraction pattern, or an image with spot. I am trying to rotate the image 360-degrees so each spot will create a ring, where I may then determine the radius of the newly-created ring.

Each image that I rotate and save will go into a stack, where I would use “Z Project” to make the stack of images into one, creating the rings.

Would your video be able to do this? If I could figure out or learn how to automatically save images without manually typing in their name, a lot of time would also be saved.

Also, I found out that rotating the image seems to zoom out of the original image. I assume to keep the pixels constant. Does your video help out with this at all?

Thank you for your help! I appreciate it.

All the best,

Ryan

1 Like

Absolutely! Try the macro linked above with your image instead of blobs.gif. Afterwards, run the Plugins > ImageJ on GPU (clij) > Projections > Maximum-Z Projection on GPU menu and see what gets recorded in the macro recorder.

Same for saving: Save an image an see what gets recorded. In that way it’s pretty easy to pick up the ImageJ macro language. :wink:

I also have an example macro that goes through a folder and processes some images and saves them using the saveAs macro call:

Cheers,
Robert

Hi Robert,

I hope that you had a great weekend!

I will definitely check these out during the day, unless work gets backed up. I will keep you updated.

Thanks again for your help!

All the best,

Ryan

1 Like