Calling a python

Ok, trying to learn enough python so as to get 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?

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()

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 - *
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 import FileSaver
import sys
import NativeCodeJNA
import InverseAnscombe
from ij import IJ, WindowManager


# 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, “32-bit”, “”);, “Add…”, “value=0.375 stack”);, “Square Root”, “stack”);, “Multiply…”, “value=2 stack”);, “32-bit”, “”);, “Add…”, “value=0.375 stack”);, “Square Root”, “stack”);, “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()


# 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)

fs = FileSaver(outputImp)


InputImg.close();“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?


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:“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.