# HG changeset patch # User Sebastien Jodogne # Date 1623337634 -7200 # Node ID f4fc12ed00ee4fe959b50a5e6c43c3241768a9e3 # Parent bcd1a6a89280715a7f2a73243e9916266d70bfdb creating dicom instances in Python diff -r bcd1a6a89280 -r f4fc12ed00ee Sphinx/source/plugins/python.rst --- 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 ---------------------------