This describes the scripting in MRIcroGL 1.2 and later. Click here for older versions
MRIcroGL allows the user to run scripts written in the Python language. This is useful to illustrate features or automate laborious and repetitive tasks. The scripts can be entered in the graphical interface (the Scripting/New menu item) or via the command line.
Specific considerations for Python
- Each Python script should include 'import gl' - this provides access to the MRIcroGL built-in functions.
- Python uses '#' for comments.
- Python is case sensitive, and all MRIcroGL functions are lower cased.
- Python uses "=" for assignments.
- Python uses "==" to test equality
- PyArg_ParseTuple requires that boolean values are either "0" (false) or "1" (true).
Below each of the Pascal scripts that are provided with MRIcroGL have been ported to Python.
You can edit and run scripts from the graphical interface. Choose Scripting/New to create a script and Scripting/Run to execute a script. The Scripting/Templates command provides several sample scripts.
You can also launch scripts from the command line. For example, running mricrogl myscript.py
will launch MRIcroGL and have it run the Python script myscript.py. This method allows you to invoke MRIcroGL from other programming languages.
For example, here is how you would launch MRIcroGL with Matlab:
script = 'myscript.py';
exe = 'mricrogl';
if ~exist(exe,'file'), error('Unable to find application "%s"', exe); end;
cmd = sprintf('%s "%s"', exe, script);
system(cmd);
You could also launch MRIcroGL with a startup script from Python:
import subprocess
subprocess.call(['/Users/rorden/tst/MRIcroGL12/MRIcroGL.app/Contents/MacOS/MRIcroGL','myscript.py'])
You can have a script run whenever you launch MRIcroGL. This makes it easy to specify your favorite settings. Simply put a script named "startup.py" in your MRIcroGL script folder (Mac users can see this folder by selecting the MRIcroGL application in the finder and choosing "Show Package Contents").
The following list of functions is generated by the "help.py" script listed below.
azimuthelevation (built-in function):
azimuthelevation(azi, elev) -> Sets the camera location.
backcolor (built-in function):
backcolor(r, g, b) -> changes the background color, for example backcolor(255, 0, 0) will set a bright red background
bmpzoom (built-in function):
bmpzoom(z) -> changes resolution of savebmp(), for example bmpzoom(2) will save bitmaps at twice screen resolution
cameradistance (built-in function):
cameradistance(z) -> Sets the viewing distance from the object.
clipazimuthelevation (built-in function):
clipazimuthelevation(depth, azi, elev) -> Set a view-point independent clip plane.
colorbarposition (built-in function):
colorbarposition(p) -> Set colorbar position (0=off, 1=top, 2=right).
colorbarsize (built-in function):
colorbarsize(p) -> Change width of color bar f is a value 0.01..0.5 that specifies the fraction of the screen used by the colorbar.
colorfromzero (built-in function):
colorfromzero(layer, isFromZero) -> Color scheme display range from zero (1) or from treshold value (0)?
colorname (built-in function):
colorname(layer, colorName) -> Set the colorscheme for the target overlay (0=background layer) to a specified name.
cutout (built-in function):
cutout(L,A,S,R,P,I) -> Remove sector from volume.
extract (built-in function):
extract(Otsu,Dil,One) -> Remove haze.
hiddenbycutout (built-in function):
hiddenbycutout(layer, isHidden) -> Will cutout hide (1) or show (0) this layer?
linecolor (built-in function):
linecolor(r,g,b) -> Set color of crosshairs, so "linecolor(255,0,0)" will use bright red lines.
linewidth (built-in function):
linewidth(wid) -> Set thickness of crosshairs used on 2D slices.
loadimage (built-in function):
loadimage(imageName) -> Close all open images and load new background image.
minmax (built-in function):
minmax(layer, min, max) -> Sets the color range for the overlay (layer 0 = background).
modalmessage (built-in function):
modalmessage(msg) -> Show a message in a dialog box, pause script until user presses "OK" button.
mosaic (built-in function):
mosaic(mosString) -> Create a series of 2D slices.
opacity (built-in function):
opacity(layer, opacityPct) -> Make the layer (0 for background, 1 for 1st overlay) transparent(0), translucent (~50) or opaque (100).
orthoviewmm (built-in function):
orthoviewmm(x,y,z) -> Show 3 orthogonal slices of the brain, specified in millimeters.
overlayadditiveblending (built-in function):
overlayadditiveblending(v) -> Merge overlays using additive (1) or multiplicative (0) blending.
overlaycloseall (built-in function):
overlaycloseall() -> Close all open overlays.
overlayload (built-in function):
overlayload(filename) -> Load an image on top of prior images.
overlayloadsmooth (built-in function):
overlayloadsmooth(0) -> Will future overlayload() calls use smooth (1) or jagged (0) interpolation?
overlaymaskwithbackground (built-in function):
overlaymaskwithbackground(v) -> hide (1) or show (0) overlay voxels that are transparent in background image.
quit (built-in function):
quit() -> Terminate the application.
resetdefaults (built-in function):
resetdefaults() -> Revert settings to sensible values.
savebmp (built-in function):
savebmp(pngName) -> Save screen display as bitmap. For example "savebmp('test.png')"
scriptformvisible (built-in function):
scriptformvisible (visible) -> Show (1) or hide (0) the scripting window.
shaderadjust (built-in function):
shaderadjust(sliderName, sliderValue) -> Set level of shader property. Example "gl.shaderadjust('edgethresh', 0.6)"
shaderlightazimuthelevation (built-in function):
shaderlightazimuthelevation(a,e) -> Position the light that illuminates the rendering. For example, "shaderlightazimuthelevation(0,45)" places a light 45-degrees above the object
shadername (built-in function):
shadername(name) -> Choose rendering shader function. For example, "shadername('mip')" renders a maximum intensity projection.
shaderquality1to10 (built-in function):
shaderquality1to10(i) -> Renderings can be fast (1) or high quality (10), medium values (6) balance speed and quality.
shaderupdategradients (built-in function):
shaderupdategradients() -> Recalculate volume properties.
sharpen (built-in function):
sharpen() -> apply unsharp mask to background volume to enhance edges
toolformvisible (built-in function):
toolformvisible(visible) -> Show (1) or hide (0) the tool panel.
version (built-in function):
version() -> Return the version of MRIcroGL.
viewaxial (built-in function):
viewaxial(SI) -> Show rendering with camera superior (1) or inferior (0) of volume.
viewcoronal (built-in function):
viewcoronal(AP) -> Show rendering with camera posterior (1) or anterior (0) of volume.
viewsagittal (built-in function):
viewsagittal(LR) -> Show rendering with camera left (1) or right (0) of volume.
volume (built-in function):
volume(layer, vol) -> For 4D images, set displayed volume (layer 0 = background; volume 0 = first volume in layer).
wait (built-in function):
wait(ms) -> Pause script for (at least) the desired milliseconds.
This script loads a background image and overlays. If you uncomment the last line you will see 2D slices or the image, otherwise you will see a volume rendering.
import gl
gl.resetdefaults()
#open background image
gl.loadimage('spm152')
#open overlay: show positive regions
gl.overlayload('spmMotor')
gl.minmax(1, 4, 4)
gl.opacity(1,50)
#open overlay: show negative regions
gl.overlayload('spmMotor')
gl.minmax(2, -4, -4)
gl.colorname (2,"3blue")
#gl.orthoviewmm(37,-14,47)
This script loads a CT scan and adjusts the contrast to display the kidneys.
import gl
gl.resetdefaults()
ktime= 15
ksteps= 36
gl.resetdefaults()
gl.loadimage('abdo256')
#adjust contrast to show bones
gl.minmax(0, 300, 1200)
for x in range(1, ksteps):
gl.azimuthelevation(x*10,30)
gl.wait(ktime)
#adjust color scheme to show kidneys
gl.colorname(0,'CT_Kidneys')
for x in range(1, ksteps):
gl.azimuthelevation(x*10,30)
gl.wait(ktime)
This script opens a CT scan of the head.
import gl
gl.resetdefaults()
gl.loadimage('CT_Philips')
gl.colorname(0,'CT_Bones')
This script cuts a box out of the brain. It also uses the special OverlaySurface
shader that only shows overlays that are beneath the surface of the brain.
import gl
gl.resetdefaults()
#open background image
gl.loadimage('spm152')
gl.overlayloadsmooth(1)
#open overlay: show positive regions
gl.overlayload('spmMotor')
gl.minmax(1, -2,-5)
gl.colorname(1,'8redyell')
gl.colorfromzero(1,1)
gl.overlayload('spmMotor')
gl.minmax(2, 2,5)
gl.colorname(2,'5winter')
gl.colorfromzero(2,1)
gl.hiddenbycutout(1,1)
gl.cutout(0.5,0.5,0.5,0,1,1)
gl.shadername('OverlaySurface')
gl.backcolor(255,255,255)
Lists all the in-built python functions with a brief description of each (see above for output).
import gl
print(gl.__doc__)
for key in dir( gl ):
if not key.startswith('_'):
x = getattr( gl, key ).__doc__
print(key+' (built-in function): ')
print(x)
When overlays are a different resolution than the background image they must be re-sliced to match the background. You can choose whether this used nearest-neighbor (which creates blocky artifacts) or trilinear (which creates blurry images) interpolation. This script loads two overlays, and uses different interpolation to demonstrate this choice.
import gl
gl.resetdefaults()
#open background image
gl.loadimage('spm152')
#open overlay: show positive regions
gl.overlayloadsmooth(1);
gl.overlayload('spmMotor')
gl.minmax(1, 4, 4)
gl.opacity(1,50)
#open overlay: show negative regions
gl.overlayloadsmooth(0);
gl.overlayload('spmMotor')
gl.minmax(2, -4, -4)
gl.colorname (2,"3blue")
gl.overlayloadsmooth(1);
print("n.b. Blue surface is jagged (smoothing off)");
Render an angiography image using a maximum intensity projection.
import gl
gl.resetdefaults()
gl.loadimage('chris_MRA')
#load Maximum Intensity Projection Shader
gl.shadername('MIP')
The mosaic view can show several slices of the brain simultaneously.
import gl
gl.resetdefaults()
#open background image
gl.loadimage('spm152')
#open overlay: show positive regions
gl.overlayload('spmMotor')
gl.minmax(1, 4, 4)
gl.opacity(1,50)
#open overlay: show negative regions
gl.overlayload('spmMotor')
gl.minmax(2, -4, -4)
gl.colorname (2,"3blue")
gl.mosaic("A L+ H -0.1 -24 -16 16 40; 48 56 S X R 0");
The mosaic view can also show multiple renderings at one time.
import gl
gl.resetdefaults()
gl.backcolor(255, 255, 255)
#open background image
gl.loadimage('spm152')
#open overlay: show positive regions
gl.overlayload('spmMotor')
gl.minmax(1, 4, 4)
gl.opacity(1,50)
#open overlay: show negative regions
gl.overlayload('spmMotor')
gl.minmax(2, -4, -4)
gl.colorname (2,"3blue")
gl.colorbarposition(0)
#"a"xial, "c"oronal and "s"agittal "r"enderings
gl.mosaic("A R 0 C R 0 S R 0; A R -0 C R -0 S R -0");