Mercurial > hg > orthanc-book
comparison 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 |
comparison
equal
deleted
inserted
replaced
694:bcd1a6a89280 | 695:f4fc12ed00ee |
---|---|
220 Here is a basic Python script that registers two new routes in the | 220 Here is a basic Python script that registers two new routes in the |
221 REST API:: | 221 REST API:: |
222 | 222 |
223 import orthanc | 223 import orthanc |
224 import pprint | 224 import pprint |
225 | 225 |
226 def OnRest(output, uri, **request): | 226 def OnRest(output, uri, **request): |
227 pprint.pprint(request) | 227 pprint.pprint(request) |
228 print('Accessing uri: %s' % uri) | 228 print('Accessing uri: %s' % uri) |
229 output.AnswerBuffer('ok\n', 'text/plain') | 229 output.AnswerBuffer('ok\n', 'text/plain') |
230 | 230 |
258 .. highlight:: python | 258 .. highlight:: python |
259 | 259 |
260 This sample uploads a DICOM file as soon as Orthanc is started:: | 260 This sample uploads a DICOM file as soon as Orthanc is started:: |
261 | 261 |
262 import orthanc | 262 import orthanc |
263 | 263 |
264 def OnChange(changeType, level, resource): | 264 def OnChange(changeType, level, resource): |
265 if changeType == orthanc.ChangeType.ORTHANC_STARTED: | 265 if changeType == orthanc.ChangeType.ORTHANC_STARTED: |
266 print('Started') | 266 print('Started') |
267 | 267 |
268 with open('/tmp/sample.dcm', 'rb') as f: | 268 with open('/tmp/sample.dcm', 'rb') as f: |
269 orthanc.RestApiPost('/instances', f.read()) | 269 orthanc.RestApiPost('/instances', f.read()) |
270 | 270 |
271 elif changeType == orthanc.ChangeType.ORTHANC_STOPPED: | 271 elif changeType == orthanc.ChangeType.ORTHANC_STOPPED: |
272 print('Stopped') | 272 print('Stopped') |
273 | 273 |
274 elif changeType == orthanc.ChangeType.NEW_INSTANCE: | 274 elif changeType == orthanc.ChangeType.NEW_INSTANCE: |
275 print('A new instance was uploaded: %s' % resource) | 275 print('A new instance was uploaded: %s' % resource) |
276 | 276 |
277 orthanc.RegisterOnChangeCallback(OnChange) | 277 orthanc.RegisterOnChangeCallback(OnChange) |
278 | 278 |
279 | 279 |
280 | 280 |
281 .. warning:: | 281 .. warning:: |
830 } | 830 } |
831 | 831 |
832 http.createServer(requestListener).listen(8000); | 832 http.createServer(requestListener).listen(8000); |
833 | 833 |
834 | 834 |
835 | 835 .. _python_create_dicom: |
836 | |
837 Creating DICOM instances (new in 3.1) | |
838 ..................................... | |
839 | |
840 .. highlight:: python | |
841 | |
842 The following sample Python script will write on the disk a new DICOM | |
843 instance including the traditional Lena sample image, and will decode | |
844 the single frame of this DICOM instance:: | |
845 | |
846 import json | |
847 import orthanc | |
848 | |
849 def OnChange(changeType, level, resource): | |
850 if changeType == orthanc.ChangeType.ORTHANC_STARTED: | |
851 tags = { | |
852 'SOPClassUID' : '1.2.840.10008.5.1.4.1.1.1', | |
853 'PatientID' : 'HELLO', | |
854 'PatientName' : 'WORLD', | |
855 } | |
856 | |
857 with open('Lena.png', 'rb') as f: | |
858 img = orthanc.UncompressImage(f.read(), orthanc.ImageFormat.PNG) | |
859 | |
860 s = orthanc.CreateDicom(json.dumps(tags), img, orthanc.CreateDicomFlags.GENERATE_IDENTIFIERS) | |
861 | |
862 with open('/tmp/sample.dcm', 'wb') as f: | |
863 f.write(s) | |
864 | |
865 dicom = orthanc.CreateDicomInstance(s) | |
866 frame = dicom.GetInstanceDecodedFrame(0) | |
867 print('Size of the frame: %dx%d' % (frame.GetImageWidth(), frame.GetImageHeight())) | |
868 | |
869 orthanc.RegisterOnChangeCallback(OnChange) | |
870 | |
871 | |
872 | |
873 | |
836 | 874 |
837 Performance and concurrency | 875 Performance and concurrency |
838 --------------------------- | 876 --------------------------- |
839 | 877 |
840 **Important:** This section only applies to UNIX-like systems. The | 878 **Important:** This section only applies to UNIX-like systems. The |