changeset 708:cd70d23f34bc

Lookup DICOM dictionary in Python scripts
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 11 Jun 2021 14:09:21 +0200
parents daf07750e901
children 9d5e17100a8d
files Sphinx/source/plugins/python.rst Sphinx/source/plugins/python/lookup-dictionary.py
diffstat 2 files changed, 46 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst	Fri Jun 11 12:44:56 2021 +0200
+++ b/Sphinx/source/plugins/python.rst	Fri Jun 11 14:09:21 2021 +0200
@@ -479,8 +479,6 @@
 .. literalinclude:: python/authorization-2.py
                     :language: python
 
-.. highlight:: javascript
-
 This filter could be used together with the following Web service
 implemented using `Node.js
 <https://en.wikipedia.org/wiki/Node.js>`__:
@@ -489,6 +487,34 @@
                     :language: javascript
 
   
+.. _python_lookup_dictionary:
+
+Lookup DICOM dictionary (new in 3.2)
+....................................
+
+Python plugins can access the dictionary of the DICOM tags that are
+handled by Orthanc:
+
+.. literalinclude:: python/lookup-dictionary.py
+                    :language: python
+
+.. highlight:: text
+
+Note how Python introspection is used in order to map the values in
+enumeration ``orthanc.ValueRepresentation`` to a string description of
+the value representation. If started, the plugin above would output
+the following information in the Orthanc logs::
+
+  W0611 14:04:08.563957 PluginsManager.cpp:168] Entry in the dictionary: {
+      "Element": 32, 
+      "Group": 16, 
+      "MaxMultiplicity": 1, 
+      "MinMultiplicity": 1, 
+      "ValueRepresentation": 11
+  }
+  W0611 14:04:08.563975 PluginsManager.cpp:168] Name of the value representation: LO
+
+
 .. _python_create_dicom:
 
 Creating DICOM instances (new in 3.2)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sphinx/source/plugins/python/lookup-dictionary.py	Fri Jun 11 14:09:21 2021 +0200
@@ -0,0 +1,18 @@
+import json
+import orthanc
+
+# Create a dictionary mapping the numeric values in enumeration
+# "orthanc.ValueRepresentation" to the name of the corresponding VR
+VR_NAMES = {}
+for name in dir(orthanc.ValueRepresentation):
+    if not name.startswith('_'):
+        value = getattr(orthanc.ValueRepresentation, name)
+        VR_NAMES[value] = name
+
+entry = orthanc.LookupDictionary('PatientID')
+
+orthanc.LogWarning('Entry in the dictionary: %s' %
+                   json.dumps(entry, indent = 4, sort_keys = True))
+
+orthanc.LogWarning('Name of the value representation: %s' %
+                   VR_NAMES[entry['ValueRepresentation']])