Mercurial > hg > orthanc
annotate Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 2126:03b065778fc3
cleaning up
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 07 Nov 2016 13:47:21 +0100 |
parents | 65b1ce7cb84f |
children | a3a65de1840f |
rev | line source |
---|---|
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
3 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
747
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
6 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
7 # This program is free software: you can redistribute it and/or |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
8 # modify it under the terms of the GNU General Public License as |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
9 # published by the Free Software Foundation, either version 3 of the |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
10 # License, or (at your option) any later version. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
11 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, but |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
13 # WITHOUT ANY WARRANTY; without even the implied warranty of |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
15 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
16 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
17 # You should have received a copy of the GNU General Public License |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
19 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
733
diff
changeset
|
20 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 import os |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 import sys |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 import os.path |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 import httplib2 |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
25 import base64 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 |
370 | 27 if len(sys.argv) != 4 and len(sys.argv) != 6: |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 print(""" |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 Sample script to recursively import in Orthanc all the DICOM files |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 that are stored in some path. Please make sure that Orthanc is running |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 before starting this script. The files are uploaded through the REST |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 API. |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 Usage: %s [hostname] [HTTP port] [path] |
370 | 35 Usage: %s [hostname] [HTTP port] [path] [username] [password] |
2032
65b1ce7cb84f
Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
36 For instance: %s 127.0.0.1 8042 . |
370 | 37 """ % (sys.argv[0], sys.argv[0], sys.argv[0])) |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 exit(-1) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
42 success_count = 0 |
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
43 total_file_count = 0 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 # This function will upload a single file to Orthanc through the REST API |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 def UploadFile(path): |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
48 global success_count |
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
49 global total_file_count |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 |
370 | 51 f = open(path, "rb") |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 content = f.read() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 f.close() |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
54 total_file_count += 1 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 try: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 sys.stdout.write("Importing %s" % path) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 h = httplib2.Http() |
370 | 60 |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
61 headers = { 'content-type' : 'application/dicom' } |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
62 |
370 | 63 if len(sys.argv) == 6: |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
64 username = sys.argv[4] |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
65 password = sys.argv[5] |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
66 |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
67 # h.add_credentials(username, password) |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
68 |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
69 # This is a custom reimplementation of the |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
70 # "Http.add_credentials()" method for Basic HTTP Access |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
71 # Authentication (for some weird reason, this method does |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
72 # not always work) |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
73 # http://en.wikipedia.org/wiki/Basic_access_authentication |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
74 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password) |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
75 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 resp, content = h.request(URL, 'POST', |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 body = content, |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
78 headers = headers) |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 if resp.status == 200: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 sys.stdout.write(" => success\n") |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
82 success_count += 1 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 else: |
1766
d178fab186dd
reminder of password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
84 sys.stdout.write(" => failure (Is it a DICOM file? Is there a password?)\n") |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 except: |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
87 sys.stdout.write(" => unable to connect (Is Orthanc running? Is there a password?)\n") |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 if os.path.isfile(sys.argv[3]): |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 # Upload a single file |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 UploadFile(sys.argv[3]) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 else: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 # Recursively upload a directory |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 for root, dirs, files in os.walk(sys.argv[3]): |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 for f in files: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 UploadFile(os.path.join(root, f)) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 |
733
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
99 if success_count == total_file_count: |
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
100 print("\nSummary: all %d DICOM file(s) have been imported successfully" % success_count) |
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
101 else: |
29982cfc5009
improvements to ImportDicomFiles tx Karsten Hilbert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
373
diff
changeset
|
102 print("\nSummary: %d out of %d files have been imported successfully as DICOM instances" % (success_count, total_file_count)) |