comparison OrthancServer/Resources/Samples/Python/DicomizeImage.py @ 5240:c9e2c6d1cd62

added two Python samples: DicomizeImage.py and MicroCTDicomization.py
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Apr 2023 17:54:21 +0200
parents
children 59e3b6f8c5be
comparison
equal deleted inserted replaced
5235:5a1e81eef654 5240:c9e2c6d1cd62
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Orthanc - A Lightweight, RESTful DICOM Store
5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
6 # Department, University Hospital of Liege, Belgium
7 # Copyright (C) 2017-2023 Osimis S.A., Belgium
8 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
9 #
10 # This program is free software: you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation, either version 3 of the
13 # License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23
24 #
25 # This sample Python script illustrates how to DICOM-ize a JPEG image,
26 # a PNG image, or a PDF file using the route "/tools/create-dicom" of
27 # the REST API of Orthanc. Make sure to adapt the parameters of the
28 # DICOM-ization below.
29 #
30 # The following command-line will install the required library:
31 #
32 # $ sudo pip3 install requests
33 #
34
35 import base64
36 import imghdr
37 import json
38 import os
39 import requests
40
41
42 ########################################
43 ## Parameters for the DICOM-ization ##
44 ########################################
45
46 PATH = os.path.join(os.getenv('HOME'), 'Downloads', 'Spy 11B.jpg')
47
48 URL = 'http://localhost:8042/'
49 USERNAME = 'orthanc'
50 PASSWORD = 'orthanc'
51
52 TAGS = {
53 'ConversionType' : 'SI', # Scanned Image
54 'InstanceNumber' : '1',
55 'Laterality' : '',
56 'Modality' : 'OT',
57 'PatientOrientation' : '',
58 'SOPClassUID' : '1.2.840.10008.5.1.4.1.1.7', # Secondary Capture Image Storage
59 'SeriesNumber' : '1',
60 }
61
62 if True:
63 # Case 1: Attach the new DICOM image as a new series in an
64 # existing study. In this case, "PARENT_STUDY" indicates the
65 # Orthanc identifier of the parent study:
66 # https://book.orthanc-server.com/faq/orthanc-ids.html
67 PARENT_STUDY = '66c8e41e-ac3a9029-0b85e42a-8195ee0a-92c2e62e'
68
69 else:
70 # Case 2: Create a new study
71 PARENT_STUDY = None
72 STUDY_TAGS = {
73 'PatientID' : 'Test',
74 'PatientName' : 'Hello^World',
75 'PatientSex' : 'O',
76
77 'PatientBirthDate' : None,
78 'StudyID' : 'Test',
79 'ReferringPhysicianName' : None,
80 'AccessionNumber' : None,
81 }
82
83 TAGS.update(STUDY_TAGS)
84
85
86
87 ########################################
88 ## Application of the DICOM-ization ##
89 ########################################
90
91 if imghdr.what(PATH) == 'jpeg':
92 mime = 'image/jpeg'
93 elif imghdr.what(PATH) == 'png':
94 mime = 'image/png'
95 elif os.path.splitext(PATH) [1] == '.pdf':
96 mime = 'application/pdf'
97 else:
98 raise Exception('The input image is neither JPEG, nor PNG, nor PDF')
99
100 with open(PATH, 'rb') as f:
101 content = f.read()
102
103 data = 'data:%s;base64,%s' % (mime, base64.b64encode(content).decode('ascii'))
104
105 arguments = {
106 'Content': data,
107 'Tags': TAGS,
108 }
109
110 if PARENT_STUDY != None:
111 arguments['Parent'] = PARENT_STUDY
112
113 r = requests.post('%s/tools/create-dicom' % URL,
114 json.dumps(arguments),
115 auth = requests.auth.HTTPBasicAuth(USERNAME, PASSWORD))
116 r.raise_for_status()