changeset 1535:34c8954544e8

sample Lua script to invoke ImageJ on DICOM images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 13 Aug 2015 13:41:59 +0200
parents 95b3b0260240
children 1b03676d68c2
files Resources/Samples/Lua/CallImageJ.lua
diffstat 1 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/Lua/CallImageJ.lua	Thu Aug 13 13:41:59 2015 +0200
@@ -0,0 +1,35 @@
+-- This sample shows how to invoke an ImageJ script on every DICOM
+-- image received by Orthanc. The ImageJ script is generated by the
+-- "Initialize()" function at the startup of Orthanc. Whenever a new
+-- instance is received, its DICOM file is stored into a temporary
+-- file, and a system call to ImageJ is triggered.
+
+SCRIPT = 'ImageJScript.txt'
+
+function Initialize()
+   local target = assert(io.open(SCRIPT, 'w'))
+
+   -- This is a sample ImageJ script that display the size of the DICOM image
+   target:write('if (getArgument=="") exit ("No argument!");\n')
+   target:write('open(getArgument);\n')
+   target:write('print(getTitle + ": " + getWidth + "x" + getHeight);\n')
+
+   target:close()
+end
+
+function OnStoredInstance(instanceId)
+   -- Retrieve the DICOM instance from Orthanc
+   local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
+
+   -- Write the DICOM content to some temporary file
+   local path = instanceId .. '.dcm'
+   local target = assert(io.open(path, 'wb'))
+   target:write(dicom)
+   target:close()
+
+   -- Call ImageJ
+   os.execute('imagej -b ' .. SCRIPT .. ' ' .. path)
+
+   -- Remove the temporary DICOM file
+   os.remove(path)
+end