IJP Scala Console is simple user interface for executing Scala scripts.
Contents
The Scala Console can be run stand-alone, embedded in a desktop application, or ImageJ plugin. UI is build with ScalaFX.
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
.
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 imageopenImage
- load image from a filerun
- run a plugin or a commandsave
- save current imagesetSlice
- select slice in current imageshowMessage
- 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)
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.
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()
- ScalaInterpreterPane - a Swing component for editing code in the Scala programming language and executing it in an interpreter.
- Scala Scripting - a library providing a JSR-223-compliant scripting plugin for the Scala language, part of SciJava Script Editor project. The project support multiple scripting languages. Detailed info can be found at Using the Script Editor wiki.
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