diff Resources/Samples/Lua/WriteToDisk.lua @ 1465:905842836ad4

sample Lua script to write DICOM series to disk
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 27 Jul 2015 17:33:47 +0200
parents
children 4555a8ef2e88
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/Lua/WriteToDisk.lua	Mon Jul 27 17:33:47 2015 +0200
@@ -0,0 +1,34 @@
+TARGET = '/tmp/lua'
+
+function ToAscii(s)
+   -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
+   return s:gsub('[^a-zA-Z0-9-/ ]', '_')
+end
+
+function OnStableSeries(seriesId, tags, metadata)
+   print('This series is now stable, writing its instances on the disk: ' .. seriesId)
+
+   local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances']
+   local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags']
+   local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags']
+   local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags']
+
+   for i, instance in pairs(instances) do
+      local path = ToAscii(TARGET .. '/' .. 
+                              patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' ..
+                              study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' ..
+                              series['SeriesDescription'])
+
+      -- Retrieve the DICOM file from Orthanc
+      local dicom = RestApiGet('/instances/' .. instance .. '/file')
+
+      -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
+      -- http://stackoverflow.com/a/16029744/881731
+      os.execute('mkdir -p "' .. path .. '"')
+
+      -- Write to the file
+      local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb'))
+      target:write(dicom)
+      target:close()
+   end
+end