Run ImageJ macros in GUI mode using python without JDK

fiji
python
#1

Hi,

I am building a small portable application in python (based on PyQt) that will load some processed images into a portable imageJ. I have these image files placed inside a local folder and the values of scales for each of these images were recognized using an OCR tool. Now I want to open these files in the local ImageJ executable and run dynamically generated macros with changing fields of ‘location’, ‘x’ and ‘y’ values.

open(“C:\image\directory\location”);
run(“Set Scale…”, “distance=“x” known=“y” unit=microns”);

I need to operate the imageJ application remotely in a traditional UI mode since the user wants to do further adjustments to the file. The biggest limitation in my case is that I cannot install Java or Visual Studio in my workstation and therefore cannot use the existing imagej libraries that directly connect python script and my application. Therefore, I was hoping for some creative solutions.

So far, I can open the application from python and can generate macro codes for each file/folder. Is it currently possible to load this generated macro into a running ImageJ application using a bash script or python code? This can eliminate the need for the user to select the macro and run them for each image.

#2

You might try to run ImageJ in headless mode, see:

https://imagej.nih.gov/ij/docs/guide/146-18.html


https://code.adonline.id.au/imagej-batch-process-headless/

Then you can call Imagej with an exec command from python.

#3

Hi,

Could you kindly elaborate on that last sentence? I can run the macros in a headless mode even using a python library called fijibin. But is it possible to open the ImageJ graphical interface with these changes after macro by running the following command?

os.system(“start C:\location\ImageJ-win64.exe”)

#4

There is also a way to start ImageJ1 and execute a macro with:

https://imagej.nih.gov/ij/macros/CommandLine.txt
https://imagej.nih.gov/ij/docs/install/linux.html#options

or try (please translate execute command to python):

execute(“path_to_IJ path_to_script”)

see, e.g.,:

It should at least work on Windows (I implemented this as an option in my IJ macro editor!)

#5

Thank you for the suggestions. Unfortunately, I am still confused about an efficient way to implement them together in my code. I have tried to convey my original idea by posting it as a simple python code snippet below.

https://pastebin.com/hYx0d37H

#6

If you give the second argument (path to macro file) in the subprocess call (line 15) as I described it for Java in my previous post does it work then?

See:

Soemthing like:

subprocess.call(path_to_ImageJ path_to_file)

subprocess.call([path_to_imageJ,path_to_file])

#7

I sucessfully called a macro from Python on Windows (ImageJ GUI opened) with this command:

import subprocess

subprocess.call(['C:\\Users\\Public\\ij150-win-java8\\ImageJ\\ImageJ.exe','C:\\Users\\Public\\ij150-win-java8\\ImageJ\\macros\\RandomOvals.ijm'])

See this discussion:

1 Like
#8

I followed the same command as you did, but I am only able to open the ImageJ GUI. But without the macro loaded together.

path_to_imageJ = os.path.join(os.getcwd(), “Application\Fiji.app\ImageJ-win64.exe”) # Location of ImageJ
path_to_macro = os.path.join(os.getcwd(), “Macro.ijm”) # Location of a Macro (working and tested)
subprocess.call([path_to_imageJ, path_to_macro])

Some suggestions based on other posts are:

  1. Try to run imageJ in headless mode - This does not serve my purpose, unless remaining operations can be resumed in the GUI mode
  2. Probably the required JAVA environments are not available in my system - I cannot help but overlook this requirement since I do not have permissions to install anything in my office PC

For the time being, I am limited to simply open the ImageJ GUI through my python code and ask the user to load the generated Macro for each image manually.

#9

As I said this works on Windows only. This could be a path problem of your code.

Try to open my example with a full path of yours. Try to use double slashes as given in the example
because I’m not sure if it work with forward slashes as in Java.

Sometimes minor problems (paths) causes massive headaches.

#10

You are absolutely right. I think its just the variable names that brings the error in my case. Especially one of my containing folder has a space in between. But specifying full path name helps.

cmd = [r"C:\Users\Downloads#Project Image Analyzer\Application\Fiji.app\ImageJ-win64.exe", “-macro”, r"C:\Users\Downloads#Project Image Analyzer\Application\Fiji.app\Macro.ijm"]

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

if p.wait() > 0:
print(“Error”)
else:
print(“Success”)

As you can see, i am trying to save my code inside a portable directory. And this is important since I wish to share it to many users in our network. So my task has now reduced to finding an ideal way to use variable path name within the subprocess function call. I thank you for all your support.