Calling a python

Ok, trying to learn enough python so as to get candleJ.py code to run in batch or macro mode; very newbie

I more-or-less hacked it to something callable via more-familiar macro code, but
The input image seems locked upon completion and can’t be closed.
I input an open IJ image via InputImg = IJ.getImage(); {with and without the semicolon}
I’ve tried InputImg.close() but it doesn’t close the input, though the method does close my output image OK.

Any ideas why the input window is locked and how to close it? Can this be made virtual, for example? I’m missing the function call… Is there a list somewhere perhaps?
thanks

You don’t need semicolons in python.

So do you have any error messages?

the following code should close your image.


from ij import IJ

inputimg = IJ.getImage()
inputimg.close()

If you don’t get any error message, and you image is not closing I don’t know what happens…

I tried that, so something odd must be happening [no errors, just no ability to close the input images short of killing imageJ]

My crudely hacked code should reproduce the issue with any open image (stack) as input.
see below

‘’’


Haider Khan - haiderriazkhan@hotmail.com *
Ruthazer Lab, Montreal Neurological Institute. *
McGill University *
*
Copyright © 2015 Haider Riaz Khan *
*


The CANDLE algorithm is described in: *
*
P. Coupe, Martin Munz, Jose V.Manjon, Edward Ruthazer, D.Louis Collins.*
A CANDLE for a deeper in-vivo insight. Medical Image Analysis, *
16(4):849-64 (2012). *


mods V.Bindokas for macro or batchmode
’’’

from ij import IJ, ImageStack, ImagePlus
from ij.plugin import Filters3D
from ij.process import StackStatistics
import array
import time
from ij.gui import GenericDialog
from ij.io import FileSaver
import sys
import NativeCodeJNA
import InverseAnscombe
from ij import IJ, WindowManager

beta=0.05
patchradius=1
searchradius=5

# get input image 

InputImg = IJ.openImage();

InputImg = IJ.getImage()

titl = InputImg.getTitle()

# Get Input Image Statistics  

InStats = StackStatistics(InputImg)

print “mean:”, InStats.mean, “minimum:”, InStats.min, “maximum:”, InStats.max

# get the image stack within the ImagePlus 

InputStack = InputImg.getStack()
z = InputImg.getNSlices()

# Instantiate 3D Median Filter plugin  

f3d = Filters3D()

# Start of 3D Median Filter
 
# Retrieve filtered stack 

medianFilteredStack = f3d.filter(InputStack, f3d.MEDIAN, 2, 2, 2)

# Construct an ImagePlus from the filtered stack 

medianFilteredImage = ImagePlus(“MedianFiltered-Image”, medianFilteredStack)
# End of 3D Median Filter

# get image dimensions

x = medianFilteredImage.width
y = medianFilteredImage.height

# Get Image Statistics after Median 3D Filter

medianFilterStats = StackStatistics(medianFilteredImage)

# Anscombe transform to convert Poisson noise into Gaussian noise

IJ.run(medianFilteredImage, “32-bit”, “”);
IJ.run(medianFilteredImage, “Add…”, “value=0.375 stack”);
IJ.run(medianFilteredImage, “Square Root”, “stack”);
IJ.run(medianFilteredImage, “Multiply…”, “value=2 stack”);

IJ.run(InputImg, “32-bit”, “”);
IJ.run(InputImg, “Add…”, “value=0.375 stack”);
IJ.run(InputImg, “Square Root”, “stack”);
IJ.run(InputImg, “Multiply…”, “value=2 stack”);

# End of Anscombe transform

Stats = StackStatistics(medianFilteredImage)

medianFilteredStack = medianFilteredImage.getStack()
InputStack = InputImg.getStack()

# Get the Input and filtered Images as 1D arrays

medfiltArray = array.array(‘f’)
InputImgArray = array.array(‘f’)

for i in xrange(1 , z + 1):
ip = medianFilteredStack.getProcessor(i).convertToFloat()
ip2 = InputStack.getProcessor(i).convertToFloat()
pixels = ip.getPixels()
pixels2 = ip2.getPixels()
medfiltArray.extend(pixels)
InputImgArray.extend(pixels2)

InputImg.flush()

# Noise Estimation and Non-Local Means Filter

fimg = NativeCodeJNA.NativeCall(InputImgArray, medfiltArray, int(searchradius), int(patchradius), beta , int(x), int(y), int(z))

# Optimal Inverse Anscombe Transform

fimg = InverseAnscombe.OVST(fimg)

outputstack = ImageStack(x, y, z )

for i in xrange(0, z):
# Get the slice at index i and assign array elements corresponding to it.
outputstack.setPixels(fimg[int(ixy):int((i+1)xy)], i+1)

outputImp = ImagePlus((“cndl-”+titl), outputstack)

print “OutputImage Stats:”

Stats = StackStatistics(outputImp)

print “mean:”, Stats.mean, “minimum:”, Stats.min, “maximum:”, Stats.max

outputImp.setDisplayRange(Stats.min, Stats.max)
outputImp.show()

fs = FileSaver(outputImp)
fs.save()

outputImp.close()

InputImg.close();
IJ.run(“Close All”, “”);

I’m not so familiar with python. But what happens when you remove the semicolons at the end? I think that Python can’t work with them.

In my experience all semicolons are ignored by Jython. They should not be the reason for the described problem.

@Vytas can you please wrap the code in 3 backticks to make it a code block (see for more details on Markdown).
You can try a print InputImg.isLocked() to see if this is the reason for not being able to close it.

Hi @Vytas,

at some point in the code you call InputImg.flush() which is meant to release memory and get rid of the image. However, this also may prevent closing its window. Could you please comment out that line and try again?

Cheers,
Robert

Dear Robert
Thanks, commenting out the flush() line did solve the locking problem.
Any way to have the close() function be automatic vs needing a confirmation? (image has been changed by script)

thanks again,

To answer my own question: IJ.run(“Close All”, “”) does the trick (vs imp.close() calls)

1 Like

ImagePlus uses the public field changes (see JavaDoc) for this purpose. Just run InputImg.changes = False to get rid of the confirmation dialog.