ijp-scala-console

IJP Scala Console is simple user interface for executing Scala scripts.

Actions Status Maven Central Scaladoc

Contents

Overview

The Scala Console can be run stand-alone, embedded in a desktop application, or ImageJ plugin. UI is build with ScalaFX.

Screenshot

ImageJ Plugin Download

Binaries can be downloaded from the releases page. Extract the binaries to the ImageJ plugins directory. The plugin will be available through ImageJ menu: Plugins > Scripting > Scala Console.

ImageJ Script Examples

Get Access to the Current Image

Assume that you opened an image in ImageJ, for instance, using File > Open Samples > Leaf

The following script will get a handle to the current activa image (IJ.getImage), assign it to a value imp, and then print it:

import ij.IJ

val imp = IJ.getImage
println(imp)

Note: if there is no image open a "No image" dialog will be shown and execution will be aborted

Note: that the first thing the script does is to import ImageJ's object IJ from package ij. Object IJ contains many frequently used ImageJ methods, like:

  • getImage - get current image
  • openImage - load image from a file
  • run - run a plugin or a command
  • save - save current image
  • setSlice - select slice in current image
  • showMessage - display dialog with a message

Full list can be found here: https://imagej.nih.gov/ij/developer/api/ij/ij/IJ.html

Note: you can use methods contained in IJ directly, without prefixing with IJ. To do that import a specific method, for instance, import ij.IJ.getImage or all the available methods import ij.IJ.*. Here is a shorted version of the above example:

import ij.IJ.*

println(getImage)

Run Command "Median..." to Process Current Image

You can execute any menu command using IJ.run method and providing command name. In simplest form you only provide command name, it will run on the current open image:

import ij.IJ.*

run("Median...")

The command may open additional dialog asking for options. If you know what options you want to pass you can do that:

import ij.IJ.*

run("Median...", "radius=4")

If you want to control on which image the command runs, you can do that too:

import ij.IJ.*

val imp = getImage
run(imp, "Median...", "radius=4")

The options are listed in a single string using names of fields in the dialog. For boolean values, you use only filed name if value is true (checkbox is checked), you skip the field name of value is false.

Hint: You can use Macro Recorder (Plugins > Macros > Record) to record a command then copy it to your script.

Additional example scripts can be found the examples directory.

More examples

Create Image of a Sphere

Creates an image of a sphere using discontinuity in "Red/Green" lookup table:

import ij.*
import ij.process.*

def sphere(): Unit = {
  val size = 1024
  val ip   = new FloatProcessor(size, size)
  val t0   = System.currentTimeMillis()
  for (y <- 0 until size) {
    val dy = y - size / 2
    for (x <- 0 until size) {
      val dx = x - size / 2
      val d  = Math.sqrt(dx * dx + dy * dy).toFloat
      ip.setf(x, y, -d)
    }
  }
  val time = (System.currentTimeMillis() - t0) / 1000
  val img  = new ImagePlus(s"$time seconds", ip)
  // Apply "Red/Green" lookup table
  IJ.run(img, "Red/Green", "")
  img.show()
}

sphere()

example sphere

Related Projects

License

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA