Difference between revisions of "TrakEM2 Scripting"

Line 77: Line 77:
  
  
=== The properties of a Displayable: color, visibil
+
=== The properties of a Displayable: title, color, visibility, locked, alpha, affine transform, dimensions and bounds ===
 +
 
 +
Let's set a few values:
 +
 
 +
<source lang="python">
 +
>>> p = Display.getFront().getActive()
 +
>>> p.title = "Test image"
 +
>>> p.alpha = 0.4
 +
>>> p.visible = True
 +
>>> p.locked = False
 +
>>> from java.awt import Color
 +
>>> p.color = Color.blue
 +
</source>
 +
 
 +
Let's read a few values:
 +
 
 +
<source lang="python">
 +
>>> p.getAffineTransform()
 +
AffineTransform[[1.0, 0.0, 474.0], [0.0, 1.0, 567.0]]
 +
>>> print p.getBoundingBox()
 +
java.awt.Rectangle[x=474,y=567,width=2048,height=2048]
 +
>>>
 +
</source>e)
 +
 
 +
The affine transform cannot be set, because it's a final member. But itself the value may be edited via <i>setAffineTransform</i>:
 +
 
 +
<source lang="python">
 +
>>> from java.awt.geom import AffineTransform
 +
>>> aff = AffineTransform()
 +
>>> aff.scale(2.0, 2.0)
 +
>>> p.setAffineTransform(aff)
 +
</source>
 +
 
 +
Be careful: java's AffineTranform does concatenations and not pre-concatenations (order matters in matrix multiplication).
 +
 
 +
In most occasions, what you want can be accomplished with <i>[http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Displayable.html#preTransform(java.awt.geom.AffineTransform,%20boolean) preTransform]</i>, such as translating an image:
 +
 
 +
<source lang="python">
 +
>>> from java.awt.geom import AffineTransform
 +
>>> aff = AffineTransform()
 +
>>> aff.translate(300, -400)
 +
>>> p.preTransform(aff, True)
 +
</source>
 +
 
 +
More convenient are the methods <i>scale</i>, <i>translate</i>, <i>rotate</i> and particularly <i>preTransform</i>, for the manipulation of a [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Displayable.html Displayable]'s affine transform (see [http://java.sun.com/javase/6/docs/api/java/awt/geom/AffineTransform.html AffineTransform]) and that of its linked Displayables (any transform propagates to the linked ones).

Revision as of 22:05, 28 October 2009

Examples in Jython.

Open the "Plugins - Scripting - Jython Interpreter" (see Scripting Help) and make sure there is a TrakEM2 project open, with a display open. Then type or paste:


Get the instance of a selected image

>>> p = Display.getFront().getActive()
>>> print p
090504_0314_ex0768.mrc z=0.0 #67398


Obtain the ImagePlus of a selected image

>>> p = Display.getFront().getActive()
>>> imp = p.getImagePlus()
>>> print imp.width, imp.height
2048 2048


Access the Layer and Selection of a Display

>>> front = Display.getFront()
>>> layer = front.getLayer()
>>> layer_set = front.getLayerSet()
>>> sel = front.getSelection()
>>> print sel.getSelected().size()
10
>>> print sel.isEmpty()
0

In Jython, 1 is True and 0 is False

Lock all selected objects

for d in Display.getFront().getSelected():
  d.setLocked(True)

Obtain a collection of selected images

The Selection object of a Display can return a number of collections with any selected objects in it, for example of type Patch (those that wrap an image). All you need to do is to call getSelected with the name of the class to filter for:

for d in Display.getFront().getSelected(Patch):
  print d.title


Setting and getting member objects in jython

In Jython as in Python, member objects have automatically get</> and <i>set functions.

For example, altough a Displayable has a private String title member, this is valid python code for getting and setting the title of a Displayable like a Patch:

>>> p = Display.getFront().getActive()
>>> print p.title
090504_0314_ex0768.mrc

Above, a Patch takes as title the name of the file containing the ImagePlus, by default. Let's change the title to something else:

>>> p = Display.getFront().getActive()
>>> p.title = "A new name for this Patch"
>>> print p.title
A new name for this Patch


The properties of a Displayable: title, color, visibility, locked, alpha, affine transform, dimensions and bounds

Let's set a few values:

>>> p = Display.getFront().getActive()
>>> p.title = "Test image"
>>> p.alpha = 0.4
>>> p.visible = True
>>> p.locked = False
>>> from java.awt import Color
>>> p.color = Color.blue

Let's read a few values:

>>> p.getAffineTransform()
AffineTransform[[1.0, 0.0, 474.0], [0.0, 1.0, 567.0]]
>>> print p.getBoundingBox()
java.awt.Rectangle[x=474,y=567,width=2048,height=2048]
>>> 
e)

The affine transform cannot be set, because it's a final member. But itself the value may be edited via setAffineTransform:

>>> from java.awt.geom import AffineTransform
>>> aff = AffineTransform()
>>> aff.scale(2.0, 2.0)
>>> p.setAffineTransform(aff)

Be careful: java's AffineTranform does concatenations and not pre-concatenations (order matters in matrix multiplication).

In most occasions, what you want can be accomplished with preTransform, such as translating an image:

>>> from java.awt.geom import AffineTransform
>>> aff = AffineTransform()
>>> aff.translate(300, -400)
>>> p.preTransform(aff, True)

More convenient are the methods scale, translate, rotate and particularly preTransform, for the manipulation of a Displayable's affine transform (see AffineTransform) and that of its linked Displayables (any transform propagates to the linked ones).