First steps with pyimagej after installing: Applying run_macro to image

My Status

In the last couple of days, thanks to this post I managed to install pyimagej using pip (not conda) and get it to run on my machine, i.e. imagej.init() runs, and I can reproduce some of the most early steps with using ij.io().open(str(img_path)) convert it to numpy and display it using matplotlib.pyplot.imshow(). So this post is about using pyimagej as opposed to installing it.

What I want to do

I have a pretty basic image processing task that I like to process in batch. It will involve, notoriously to convert to 8bit, apply statistical region merging, thresholding, a couple of binary operation (fill holes, erode, dilate) and finally run ‘analyze particles’ - including fitted ellipses. The ellipses I’d like to get back out, together with the analysis spreadsheet so that I can use it in a pandas DataFrame.
I have already figured, that using ij.py.run_macro() I can just run commands. I can find the arguments using recording macros in Fiji.app directly. By any means I am not bound to use necessarily do this with pyimagej btw. I just know python decently and knew how to do the image processing steps I need manually in ImageJ so I thought would be convenient to use it.

Where I am stuck

I manage to open and display the images, the latter via matplotlib that is. But for example applying a simple 8bit conversion is beyond me. The Jupiter notebook that comes with pyimagej only helped me that far.

What I have done so far

Here is my code trying to apply 8bit conversion on an imported image.

#import environment.environment_macos #sets needed env vars
import imagej
import numpy
#from matplotlib import pyplot
#from misc import helper
import pathlib

# for dev purpose
#from importlib import reload

# Implement tkinter selection process
# in lieu of path selection here
img_path = pathlib.Path('/Users/maarcade/Downloads/d7')

# intializing ImageJ, first time can take a bit longer
ij = imagej.init()

# find tif images in folder
imgtype = 'tif'
img_catalog = list(img_path.glob('**/*.{}'.format(imgtype)))

# open image in ij and apply 8bit conversion
img = img_catalog[0]
ijimg = ij.io().open(str(img))
ij.py.run_macro("""run("8-bit");""") #fails 
# with JVM exception occurred: 
# java.lang.NullPointerException java.util.concurrent.ExecutionException

I am not familiar with Java, hence, the whole pyimagej :wink:, but I assume the error tells me it doesn’t know the target of the run_macro? Any pointers on how to proceed and where to learn more about pyimagej would be appreciated.

Intuitive for me would be to do something like

ijimg.run_macro()

But that’s not implemented on that class.

Welcome to the forum, @mmarras!

Thanks for posting here (and also on GitHub). Here might be a reason why you didn’t get a reply these last days:

Just be patient, I think there will soon be updated documentation as well, as we’re approaching the #pyimagej workshop at #i2k2020 :slight_smile:

1 Like