diff Sphinx/source/plugins/python.rst @ 695:f4fc12ed00ee

creating dicom instances in Python
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Jun 2021 17:07:14 +0200
parents 17c1ff4e6ae4
children 29e49f03dc27
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst	Wed Jun 09 17:35:58 2021 +0200
+++ b/Sphinx/source/plugins/python.rst	Thu Jun 10 17:07:14 2021 +0200
@@ -222,7 +222,7 @@
 
   import orthanc
   import pprint
-
+  
   def OnRest(output, uri, **request):
       pprint.pprint(request)
       print('Accessing uri: %s' % uri)
@@ -260,20 +260,20 @@
 This sample uploads a DICOM file as soon as Orthanc is started::
 
    import orthanc
-
+   
    def OnChange(changeType, level, resource):
        if changeType == orthanc.ChangeType.ORTHANC_STARTED:
            print('Started')
-
+           
            with open('/tmp/sample.dcm', 'rb') as f:
                orthanc.RestApiPost('/instances', f.read())
-        
+               
         elif changeType == orthanc.ChangeType.ORTHANC_STOPPED:
             print('Stopped')
-
+            
         elif changeType == orthanc.ChangeType.NEW_INSTANCE:
             print('A new instance was uploaded: %s' % resource)
-
+            
     orthanc.RegisterOnChangeCallback(OnChange)
 
 
@@ -832,7 +832,45 @@
   http.createServer(requestListener).listen(8000);
 
   
+.. _python_create_dicom:
 
+Creating DICOM instances (new in 3.1)
+.....................................
+
+.. highlight:: python
+
+The following sample Python script will write on the disk a new DICOM
+instance including the traditional Lena sample image, and will decode
+the single frame of this DICOM instance::
+
+  import json
+  import orthanc
+  
+  def OnChange(changeType, level, resource):
+      if changeType == orthanc.ChangeType.ORTHANC_STARTED:
+          tags = {
+              'SOPClassUID' : '1.2.840.10008.5.1.4.1.1.1',
+              'PatientID' : 'HELLO',
+              'PatientName' : 'WORLD',
+              }
+              
+          with open('Lena.png', 'rb') as f:
+              img = orthanc.UncompressImage(f.read(), orthanc.ImageFormat.PNG)
+              
+          s = orthanc.CreateDicom(json.dumps(tags), img, orthanc.CreateDicomFlags.GENERATE_IDENTIFIERS)
+          
+          with open('/tmp/sample.dcm', 'wb') as f:
+              f.write(s)
+              
+          dicom = orthanc.CreateDicomInstance(s)
+          frame = dicom.GetInstanceDecodedFrame(0)
+          print('Size of the frame: %dx%d' % (frame.GetImageWidth(), frame.GetImageHeight()))
+          
+  orthanc.RegisterOnChangeCallback(OnChange)
+
+
+
+  
 
 Performance and concurrency
 ---------------------------