Trouble converting ImagePlus object to a numpy array in Python (pyimage, jnius, numpy)

I have been playing around with using ImageJ/Fiji as a GUI for a python script.
Using pyimageJ - I can open an ImageJ instance

import imagej
ij = imagej.init('C:\Fiji.app', headless=False)
ij.ui().showUI()

#This opens an Fiji window complete with all installed Plugins, which is pretty handy.
#Any image opened in Fiji can be imported into memory for your python script by using jnius to call the java class using this: 

from jnius import autoclass
WindowManager = autoclass('ij.WindowManager')
imp = WindowManager.getCurrentImage()

#and you can visualize the imported image with:

ij.py.show(imp, cmap = 'gray')

If I try to convert to a numpy array using
 
import numpy as np
img = np.array(imp, 'float')

I cannot assign a dtype and I get an error
[TypeError: float() argument must be a string or a number, not ‘ij.ImagePlus’]

And, if I try to visualize the image with matplotlib imshow

import matplotlib as plt
plt.imshow(img)

I get this error: [TypeError: Image data can not convert to float]

Has anyone converted an ImagePlus obj. to a numpy array?
I tried looking at other ImageJ classes for conversion but I can’t find anything.

I know I can just read files directly into Python with various readers but I would really like to go back and forth from python to ImageJ to take advantage of all the plugins available.

Thanks for everyone’s time in looking at this, and providing any ideas, pointers.
Best
Graham

There are 2 methods to convert back and forth from/to Java/Python namely ij.py.to_java(PythonArray) and ij.py.from_java(ImagePlus)

However I am not sure it actually works, see the code below from this issue

import imagej
from skimage import io

ij = imagej.init()
img = io.imread('https://samples.fiji.sc/new-lenna.jpg')

inverted = ij.op().transform().invertAxisView(ij.py.to_java(img), 1)
result = ij.py.from_java(inverted)
ij.py.show(result)
2 Likes

Thanks @LThomas - it worked really well - using the ij.py.from_java() to convert the ImagePlus object to a 64bit float Python image array, which you can then do whatever you want with numpy functions
Example - below the code to let you open an image in ImageJ and convert it for use with all your Python functions that require arrays in float dtype:

#Open Fiji as a GUI
import imagej
ij = imagej.init('C:\Fiji.app', headless=False)
ij.ui().showUI()

#Use Fiji to find your image and open it
#Then grab the ImagePlus object and display it
import numpy as np
from jnius import autoclass
WindowManager = autoclass('ij.WindowManager')
imp = WindowManager.getCurrentImage()
print("ImagePlus Image")
ij.py.show(imp, cmap = 'gray')

#Convert the ImagePlus object to a Python array and display it
import matplotlib.pyplot as plt
image = ij.py.from_java(imp)
print("Image as an Python array")
print(image.dtype)
plt.imshow(img, cmap="gray")
plt.show()
1 Like

Great to see that I could help!
You can then mark my previous answer as “solution” by clicking the tick box icon :wink: that would help someone else having the same issue to spot it.

By the way you can also edit your posts and add ``` before the first line of your code and after the last line to have the code rendering. It makes it easier to read. You can even have the color syntaxing by mentioning the language after the first triple ` (ie python in this case)

1 Like