changeset 1059:2d83e9ce6bf0

added DICOM-ization of an image
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 14 Apr 2024 11:02:43 +0200
parents 30c90ac3e3d6
children a4953b6ab979
files Sphinx/source/users/advanced-rest.rst Sphinx/source/users/code/image-dicomization.py Sphinx/source/users/code/sample.png
diffstat 3 files changed, 59 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Sphinx/source/users/advanced-rest.rst	Wed Apr 10 07:54:08 2024 +0200
+++ b/Sphinx/source/users/advanced-rest.rst	Sun Apr 14 11:02:43 2024 +0200
@@ -233,22 +233,49 @@
 As of Orthanc 1.12.3, only the creation of a ZIP or a DICOMDIR archive
 produces such "outputs".
 
-  
+
+.. _image-dicomization:
+
+DICOM-ization of an image
+-------------------------
+
+JPEG images or PNG images can be converted to DICOM using the
+``/tools/create-dicom`` `route in the REST API
+<https://orthanc.uclouvain.be/api/#tag/System/paths/~1tools~1create-dicom/post>`__. The
+procedure consists in providing Orthanc with the JPEG/PNG content
+using `data URI scheme
+<https://en.wikipedia.org/wiki/Data_URI_scheme>`__. Here is a minimal
+working example in Python 3:
+
+.. literalinclude:: code/image-dicomization.py
+                    :language: python
+
+Evidently, make sure to replace ``data:image/png;base64,%s`` by
+``data:image/jpeg;base64,%s`` if you need to DICOM-ize a JPEG image
+instead of a PNG image.
+
+The call to ``/tools/create-dicom`` will return the Orthanc instance
+ID of the newly created DICOM resource. Orthanc will encode your
+JPEG/PNG using an :ref:`uncompressed transfer syntax
+<dicom-pixel-data>` to ensure maximal compatibility.
+
+Importantly, the ``Parent`` field of the ``POST`` body can be set to
+the :ref:`Orthanc identifier of some study <orthanc-ids>` in order to
+attach the newly-created PDF series to the given parent study.
+
+
 .. _pdf:
 
 Attaching PDF file as DICOM series
 ----------------------------------
 
 Among many different types of data, DICOM files can be used to store
-PDF files. The ``/tools/create-dicom`` URI can be used to upload a PDF
-file to Orthanc. The following scripts perform such a *DICOM-ization*;
-They convert the ``HelloWorld2.pdf`` file to base64, then perform a
+PDF files. Similarly to :ref:`JPEG/PNG <image-dicomization>`, the
+``/tools/create-dicom`` URI can be used to upload a PDF file to
+Orthanc. The following scripts perform such a *DICOM-ization*: They
+convert the ``HelloWorld2.pdf`` file to base64, then perform a
 ``POST`` request with JSON data containing the converted payload.
 
-Importantly, the ``Parent`` field of the ``POST`` body can be set to
-the :ref:`Orthanc identifier of some study <orthanc-ids>` in order to
-attach the newly-created PDF series to the given parent study.
-
 Using bash:
 
 .. code-block:: bash
@@ -260,7 +287,7 @@
     cat /tmp/foo | curl -H "Content-Type: application/json" -d @- http://localhost:8042/tools/create-dicom
 
 
-Using powershell:
+Using Microsoft PowerShell:
 
 .. code-block:: perl
 
@@ -282,9 +309,6 @@
 And here's another sample  `using python
 <https://github.com/orthanc-server/orthanc-setup-samples/tree/master/python-samples/attach-pdf-to-study.py>`__.
 
-Please note that the ``/tools/create-dicom`` API call will return the
-Orthanc instance ID of the newly created DICOM resource.
-
 You can use the ``/instances/.../pdf`` URI to retrieve an embedded PDF
 file.
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sphinx/source/users/code/image-dicomization.py	Sun Apr 14 11:02:43 2024 +0200
@@ -0,0 +1,22 @@
+import base64
+import json
+import requests
+
+with open('sample.png', 'rb') as f:
+    content = f.read()
+
+pixelData = base64.b64encode(content).decode('ascii')
+
+r = requests.post('http://localhost:8042/tools/create-dicom',
+                  json.dumps({
+                      'Content' : 'data:image/png;base64,%s' % pixelData,
+                      'Tags' : {
+                          'PatientName' : 'TEST',
+                          'StudyDescription' : 'MY^STUDY',
+                      }
+                  }),
+                  auth = requests.auth.HTTPBasicAuth('orthanc', 'orthanc'))
+r.raise_for_status()
+
+instanceId = r.json() ['ID']
+print('ID of the newly created DICOM instance: %s' % instanceId)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sphinx/source/users/code/sample.png	Sun Apr 14 11:02:43 2024 +0200
@@ -0,0 +1,1 @@
+../../images/Matlab.png
\ No newline at end of file