Mercurial > hg > orthanc
annotate Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 2192:61ab5fa36754 Orthanc-0.5.1
hotfix in release 0.5.1 to properly download dependencies
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 02 Dec 2016 13:04:38 +0100 |
parents | 7000726bade7 |
children | 29982cfc5009 |
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 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 import os |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 import sys |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 import os.path |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 import httplib2 |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
7 import base64 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 |
370 | 9 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
|
10 print(""" |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 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
|
12 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
|
13 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
|
14 API. |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 Usage: %s [hostname] [HTTP port] [path] |
370 | 17 Usage: %s [hostname] [HTTP port] [path] [username] [password] |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 For instance: %s localhost 8042 . |
370 | 19 """ % (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
|
20 exit(-1) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 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
|
23 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 success = 0 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 # 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
|
28 def UploadFile(path): |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 global success |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 |
370 | 31 f = open(path, "rb") |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 content = f.read() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 f.close() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 try: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 sys.stdout.write("Importing %s" % path) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 h = httplib2.Http() |
370 | 39 |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
40 headers = { 'content-type' : 'application/dicom' } |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
41 |
370 | 42 if len(sys.argv) == 6: |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
43 username = sys.argv[4] |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
44 password = sys.argv[5] |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
45 |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
46 # h.add_credentials(username, password) |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
47 |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
48 # This is a custom reimplementation of the |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
49 # "Http.add_credentials()" method for Basic HTTP Access |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
50 # Authentication (for some weird reason, this method does |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
51 # not always work) |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
52 # http://en.wikipedia.org/wiki/Basic_access_authentication |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
53 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password) |
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
54 |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 resp, content = h.request(URL, 'POST', |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 body = content, |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
57 headers = headers) |
280
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 if resp.status == 200: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 sys.stdout.write(" => success\n") |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 success += 1 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 else: |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
63 sys.stdout.write(" => failure (Is it a DICOM file?)\n") |
280
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 except: |
373
7000726bade7
fix ImportDicomFiles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
370
diff
changeset
|
66 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
|
67 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 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
|
70 # Upload a single file |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 UploadFile(sys.argv[3]) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 else: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 # Recursively upload a directory |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 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
|
75 for f in files: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 UploadFile(os.path.join(root, f)) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 print("\nSummary: %d DICOM file(s) have been imported" % success) |