]> Creatis software - gdcm.git/blob - gdcmPython/demo/PrintDicomDir.py.in
* Fix bug in the Python demo
[gdcm.git] / gdcmPython / demo / PrintDicomDir.py.in
1 import sys
2 import os.path
3 from gdcmConfigDemo import *
4 from gdcmPython.core import *
5
6 def PrintUse():
7    print ""
8    print "Use :"
9    print "-----"
10    print "%s <dicomDir>" % sys.argv[0]
11    print "   dicomDir : path to the DICOMDIR to parse"
12    print ""
13    print ""
14
15 ### Progress methods
16 def startMethod():
17    print "Start"
18 def progressMethod():
19    print "Progress",dicomdir.GetProgress()
20 def endMethod():
21    print "End"
22
23 ### Get filename from command line or default it
24 try:
25    fileName = sys.argv[1]
26 except IndexError:
27    fileName = os.path.join(GDCM_DATA_ROOT, "DICOMDIR")
28    if( not os.path.isfile(fileName) ):
29       fileName=os.path.join(os.path.split(sys.argv[0])[0],"DICOMDIR")
30
31 try:
32    printLevel = int(sys.argv[2])
33 except IndexError:
34    printLevel = 1
35
36 ### Build the DicomDir element list
37 dicomdir = gdcm.DicomDir.New()
38
39 dicomdir.SetStartMethod(startMethod)
40 dicomdir.SetProgressMethod(progressMethod)
41 dicomdir.SetEndMethod(endMethod)
42
43 dicomdir.SetFileName(fileName)
44 dicomdir.Load()
45 if not dicomdir.IsReadable():
46    PrintUse()
47    raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName
48
49 print "DICOMDIR -->",fileName
50 print "##############################################################"
51 print "## Display all the elements and their respective values"
52 print "## found in the ", fileName, " file."
53 print "##############################################################"
54 dicomdir.SetPrintLevel(-1)
55 dicomdir.Print()
56
57 print ""
58 patient=dicomdir.GetFirstPatient()
59 while(patient):
60    print "Patient"
61    study=patient.GetFirstStudy()
62    while(study):
63       print "   Study"
64       serie=study.GetFirstSerie()
65       while(serie):
66          print "      Serie"
67          image=serie.GetFirstImage()
68          while(image):
69             print "         Image"
70             print "         ---",image.GetDataEntry(0x0004,0x1500).GetString()
71             image=serie.GetNextImage()
72          serie=study.GetNextSerie()
73       study=patient.GetNextStudy()
74    patient=dicomdir.GetNextPatient()
75
76 dicomdir.SetDirectoryName(os.path.dirname(fileName))
77 dicomdir.Load()
78 if not dicomdir.IsReadable():
79    PrintUse()
80    raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName
81
82 print "DICOMDIR -->",fileName
83 print "##############################################################"
84 print "## Display all the elements and their respective values"
85 print "## found in the ", fileName, " file."
86 print "##############################################################"
87 dicomdir.SetPrintLevel(-1)
88 dicomdir.Print()
89