Examples of how to use imageJ macros
Here are some simple examples on how to use script parameters to simplify input for imageJ macros
#@ String (visibility=MESSAGE, value="PERSONAL INFO") msg1
#@ String (label = "Your name") name
#@ Date (label = "Date of birth") dob
#@ ColorRGB (label = "Favorite color", default = "255,0,204") rgb
#@ String (visibility=MESSAGE, value="PROGRAMMING SKILLS") msg2
#@ Integer(label = "Programming experience", min = 1, max = 10, style = "slider") prog_exp
#@ Boolean (label = "Process subfolders") subfolders
#@ String (visibility=MESSAGE, value="OUTPUT") msg3
#@ File (label="Select a directory", style="directory") myDir
You can write imageJ macros in any of the script languages with parameters to define input and output.
#@ String(label = "Your name") name
#@ OUTPUT String greeting
## Hello ImageJ
greeting = "Hello, " + str(name) + "!"
#print(greeting)
If you save this scirpt in the plugins folder .../Fiji.app/plugins/
you can call your script using the imageJ GUI or using the run
function in your macros
from ij import IJ
#A test dataset
test_data = ["person 1", "person 2", "person 3", "person 4"]
# make greetings
for name in test_data:
IJ.run("Hello imageJ py", "name=[" + name + "]")
Save usefull functions in modules that you can import from other imageJ macros. based on self-written-jython-modules-for-imagej
- Make a folder in the
.../Fiji.app/jars/
folder calledLib
. - save your macro containing your functions there
def helloImageJ(name):
print "hello", name
def helloImageJ_upper(name):
print "hello", (name.upper())
- restart imageJ
- import your module to a new script
import hello_imageJ_module as him
# hello_imageJ_module.py must be saved in `.../Fiji.app/jars/Lib/` folder
# List all possible functions
print dir(him)
# You can run your function like this
him.helloImageJ("normic")
him.helloImageJ_upper("normic")
if you only need one function from your module you can import only that
from hello_imageJ_module import helloImageJ
helloImageJ("normic")