Mercurial > hg > orthanc
comparison OrthancServer/Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Resources/Samples/ImportDicomFiles/ImportDicomFiles.py@c81ac6ff232b |
children | d9473bd5ed43 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
5 # Department, University Hospital of Liege, Belgium | |
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium | |
7 # | |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 | |
22 import os | |
23 import sys | |
24 import os.path | |
25 import httplib2 | |
26 import base64 | |
27 | |
28 if len(sys.argv) != 4 and len(sys.argv) != 6: | |
29 print(""" | |
30 Sample script to recursively import in Orthanc all the DICOM files | |
31 that are stored in some path. Please make sure that Orthanc is running | |
32 before starting this script. The files are uploaded through the REST | |
33 API. | |
34 | |
35 Usage: %s [hostname] [HTTP port] [path] | |
36 Usage: %s [hostname] [HTTP port] [path] [username] [password] | |
37 For instance: %s 127.0.0.1 8042 . | |
38 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) | |
39 exit(-1) | |
40 | |
41 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) | |
42 | |
43 success_count = 0 | |
44 total_file_count = 0 | |
45 | |
46 | |
47 # This function will upload a single file to Orthanc through the REST API | |
48 def UploadFile(path): | |
49 global success_count | |
50 global total_file_count | |
51 | |
52 f = open(path, "rb") | |
53 content = f.read() | |
54 f.close() | |
55 total_file_count += 1 | |
56 | |
57 try: | |
58 sys.stdout.write("Importing %s" % path) | |
59 | |
60 h = httplib2.Http() | |
61 | |
62 headers = { 'content-type' : 'application/dicom' } | |
63 | |
64 if len(sys.argv) == 6: | |
65 username = sys.argv[4] | |
66 password = sys.argv[5] | |
67 | |
68 # h.add_credentials(username, password) | |
69 | |
70 # This is a custom reimplementation of the | |
71 # "Http.add_credentials()" method for Basic HTTP Access | |
72 # Authentication (for some weird reason, this method does | |
73 # not always work) | |
74 # http://en.wikipedia.org/wiki/Basic_access_authentication | |
75 creds_str = username + ':' + password | |
76 creds_str_bytes = creds_str.encode("ascii") | |
77 creds_str_bytes_b64 = b'Basic ' + base64.b64encode(creds_str_bytes) | |
78 headers['authorization'] = creds_str_bytes_b64.decode("ascii") | |
79 | |
80 resp, content = h.request(URL, 'POST', | |
81 body = content, | |
82 headers = headers) | |
83 | |
84 if resp.status == 200: | |
85 sys.stdout.write(" => success\n") | |
86 success_count += 1 | |
87 else: | |
88 sys.stdout.write(" => failure (Is it a DICOM file? Is there a password?)\n") | |
89 | |
90 except: | |
91 type, value, traceback = sys.exc_info() | |
92 sys.stderr.write(str(value)) | |
93 sys.stdout.write(" => unable to connect (Is Orthanc running? Is there a password?)\n") | |
94 | |
95 | |
96 if os.path.isfile(sys.argv[3]): | |
97 # Upload a single file | |
98 UploadFile(sys.argv[3]) | |
99 else: | |
100 # Recursively upload a directory | |
101 for root, dirs, files in os.walk(sys.argv[3]): | |
102 for f in files: | |
103 UploadFile(os.path.join(root, f)) | |
104 | |
105 if success_count == total_file_count: | |
106 print("\nSummary: all %d DICOM file(s) have been imported successfully" % success_count) | |
107 else: | |
108 print("\nSummary: %d out of %d files have been imported successfully as DICOM instances" % (success_count, total_file_count)) |