Giter VIP home page Giter VIP logo

ijp-scala-console's Introduction

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

ijp-scala-console's People

Contributors

jpsacha avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

okwh paprikamario

ijp-scala-console's Issues

Correctly handle errors in script execution - stop the script and recover

Currently, if an error happens, "Run" buttons remains disabled and console is left in "Running" state, rather than getting back to "Ready" state.

An error may be due to ImageJ interrupting the execution. Consider example script:

import ij.IJ.*
println(getImage)

Execute the script when no image is open. This should gracefully recover after ImageJ shows "No Image" dialog.

Update examples of scripts in ReadMe

The example in ReadMe is out of date with current version of ImageJ. When there is no image, ImageJ throws exception intended to interrupt macros. It will also stop script early.

Correct the examples and add some more info

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.