Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
279:eb5fb5501569 | 280:77e526e6fdf8 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import os | |
4 import sys | |
5 import os.path | |
6 import httplib2 | |
7 | |
8 if len(sys.argv) != 4: | |
9 print(""" | |
10 Sample script to recursively import in Orthanc all the DICOM files | |
11 that are stored in some path. Please make sure that Orthanc is running | |
12 before starting this script. The files are uploaded through the REST | |
13 API. | |
14 | |
15 Usage: %s [hostname] [HTTP port] [path] | |
16 For instance: %s localhost 8042 . | |
17 """ % (sys.argv[0], sys.argv[0])) | |
18 exit(-1) | |
19 | |
20 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) | |
21 | |
22 success = 0 | |
23 | |
24 | |
25 # This function will upload a single file to Orthanc through the REST API | |
26 def UploadFile(path): | |
27 global success | |
28 | |
29 f = open(path, "r") | |
30 content = f.read() | |
31 f.close() | |
32 | |
33 try: | |
34 sys.stdout.write("Importing %s" % path) | |
35 | |
36 h = httplib2.Http() | |
37 resp, content = h.request(URL, 'POST', | |
38 body = content, | |
39 headers = { 'content-type' : 'application/dicom' }) | |
40 | |
41 if resp.status == 200: | |
42 sys.stdout.write(" => success\n") | |
43 success += 1 | |
44 else: | |
45 sys.stdout.write(" => failure (is it a DICOM file?)\n") | |
46 | |
47 except: | |
48 sys.stdout.write(" => unable to connect\n") | |
49 | |
50 | |
51 if os.path.isfile(sys.argv[3]): | |
52 # Upload a single file | |
53 UploadFile(sys.argv[3]) | |
54 else: | |
55 # Recursively upload a directory | |
56 for root, dirs, files in os.walk(sys.argv[3]): | |
57 for f in files: | |
58 UploadFile(os.path.join(root, f)) | |
59 | |
60 | |
61 print("\nSummary: %d DICOM file(s) have been imported" % success) |