Login  Register

Re: setBatchMode(True) for Python Script

Posted by Jan Eglinger on Apr 19, 2017; 12:08pm
URL: http://imagej.273.s1.nabble.com/setBatchMode-True-for-Python-Script-tp5018569p5018570.html

Dear GP,

usually, there's no need to have a batch mode with the full-featured
scripting languages, since they can work with images in variables.

For example, the IJ.run() method would always work on the image given as
first parameter:

IJ.run(imp, "Gaussian Blur...", "sigma=2")


----

However, there are cases where using IJ.run() _will_ create a new image;
in these cases you can use more low-level commands directly calling the
respective Java classes. The Macro Recorder [2] will record these when
you switch to e.g. Javascript or Beanshell mode:

imp2 = new Duplicator().run(imp, 1, 10);


----

In addition, the whole ImageJ-Ops framework [1] was created to simplify
calling commands from scripts or other commands, by clearly defining
possible inputs and outputs for each command.

----

Nevertheless, if you really _want_ to use batch mode, it is possible as
you suggested by modifying the javascript from
http://wiki.cmci.info/documents/110822jsip_cooking/javascript_imagej_cookbook#activatingdeactivating_batchmode

Here's a Python version that worked for me:

********* Python script ************
# Enabling/disabling IJ1 macro mode

from ij.macro import Interpreter as IJ1
from ij import IJ

# activate batchmode
IJ1.batchMode = True

try:
        imp = IJ.getImage()
        IJ.run(imp, "FFT", "")
        IJ.log("Done")
except(err):
        IJ1.batchMode = False;
        IJ.log("!Error with do process...")
        IJ.log(err.rhinoException.toString())
        IJ.log(err.javaException.toString())

# de-activate batchmode
IJ1.batchMode = False
************************************

Cheers,
Jan


[1]: http://imagej.net/ImageJ_Ops
[2]: http://imagej.net/Macro_recorder


On 19.04.2017 02:31, GP wrote:

> Hie!
>
> I have been converting my script from ".ijm" to ".py".
> The problem is that the output images generated from plugins pop open. I
> couldn't find any setBatchMode(true) option for python script.
> I found the discussion
> (http://forum.imagej.net/t/jython-equivalent-for-setbatchmode-true/1197/3)
> but was not able to apply to all the plugins used in this script:
>
> Following are the tasks performed over all sub-folders in the directory:
>
> 1) Get Images
> 2) Find Focussed Slice
> 3) Make Substack around it
> 4) Duplicate Substack
> 4) Process Duplicate Substack
> 5) Restore Selection on Substack
> 6) Process Original Substack
> 7) Save Final Image
>
> Script as follows:
>
> imp = IJ.openImage(images)
> title = imp.getTitle()
> IJ.run(imp, "Find focused slices", "select=100 variance=0.000")   #All image
> windows start opening from this
>
> #step onwards
> IJ.run(imp, "Make Substack...", " slices="  "")
> #makes 'Substack' around focussed slice
> Substack = IJ.getImage()
> IJ.run("Duplicate...", "duplicate")
> IJ.run("Remove Outliers...", "radius=1 threshold=50 which=Bright stack")
> IJ.run("Auto Threshold", "method=Otsu ignore_black ignore_white white")
> IJ.run("Z Project...", "projection=[Max Intensity]")
> IJ.run("Shen-Castan Edge Detector", "coefficient=0.55")
> IJ.run("Find Connected Regions", "allow_diagonal display_one_image
> display_results regions_for_values_over=100 minimum_number_of_points=1
> stop_after=-1")
> IJ.run("Ridge Detection", "line_width=3.5 high_contrast=230 low_contrast=87
> correct_position extend_line show_junction_points show_ids verbose
> displayresults add_to_manager make_binary method_for_overlap_resolution=NONE
> sigma=1.51 lower_threshold=1.06 upper_threshold=4.99")
> IJ.run("Find Maxima...", "noise=255 output=[Point Selection]")
> h = ImagePlus.getRoi(k)
> r = h.getBounds()
> IJ.makeRectangle(x-300, y-300, x-300, y-300)
> IJ.run(Substack, "Restore Selection", "")
> IJ.run(Substack, "Crop", "")
> IJ.run(Substack, "Z Project...", "projection=[Max Intensity]")
> IJ.saveAs("Tiff" , os.path.join(output_path, title))
>
> In Java API I found out batch mode possibility
> (http://wiki.cmci.info/documents/110822jsip_cooking/javascript_imagej_cookbook).
> Can this be translated to python as well?
> Please let me know if there's a way to do all the steps without having image
> windows opening.
>
>
> Thank you so much.
> Best Regards,
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html