Mercurial > hg > orthanc
annotate Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 280:77e526e6fdf8
sample script to import DICOM files in Orthanc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 10 Dec 2012 11:33:42 +0100 |
parents | |
children | 22bb88181e06 |
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 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 if len(sys.argv) != 4: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 print(""" |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 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
|
11 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
|
12 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
|
13 API. |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 Usage: %s [hostname] [HTTP port] [path] |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 For instance: %s localhost 8042 . |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 """ % (sys.argv[0], sys.argv[0])) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 exit(-1) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 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
|
21 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 success = 0 |
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 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 # 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
|
26 def UploadFile(path): |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 global success |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 f = open(path, "r") |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 content = f.read() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 f.close() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 try: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 sys.stdout.write("Importing %s" % path) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 h = httplib2.Http() |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 resp, content = h.request(URL, 'POST', |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 body = content, |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 headers = { 'content-type' : 'application/dicom' }) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 if resp.status == 200: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 sys.stdout.write(" => success\n") |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 success += 1 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 else: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 sys.stdout.write(" => failure (is it a DICOM file?)\n") |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 except: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 sys.stdout.write(" => unable to connect\n") |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 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
|
52 # Upload a single file |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 UploadFile(sys.argv[3]) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 else: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 # Recursively upload a directory |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 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
|
57 for f in files: |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 UploadFile(os.path.join(root, f)) |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 |
77e526e6fdf8
sample script to import DICOM files in Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 print("\nSummary: %d DICOM file(s) have been imported" % success) |