]> Creatis software - gdcm.git/blob - gdcmPython/demo/PrintDicomDir.py.in
* Add the CommandPy and CommandManager to the python wrapping
[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 ### Get filename from command line or default it
16 try:
17    fileName = sys.argv[1]
18 except IndexError:
19    fileName = os.path.join(GDCM_DATA_ROOT, "DICOMDIR")
20    if( not os.path.isfile(fileName) ):
21       fileName=os.path.join(os.path.split(sys.argv[0])[0],"DICOMDIR")
22
23 try:
24    printLevel = int(sys.argv[2])
25 except IndexError:
26    printLevel = 1
27
28 ### Build the DicomDir element list
29 dicomdir = gdcm.DicomDir.New()
30
31 dicomdir.SetFileName(fileName)
32 dicomdir.Load()
33 if not dicomdir.IsReadable():
34    PrintUse()
35    raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName
36
37 print "DICOMDIR -->",fileName
38 print "##############################################################"
39 print "## Display all the elements and their respective values"
40 print "## found in the ", fileName, " file."
41 print "##############################################################"
42 dicomdir.SetPrintLevel(-1)
43 dicomdir.Print()
44
45 print ""
46 patient=dicomdir.GetFirstPatient()
47 while(patient):
48    print "Patient"
49    study=patient.GetFirstStudy()
50    while(study):
51       print "   Study"
52       serie=study.GetFirstSerie()
53       while(serie):
54          print "      Serie"
55          image=serie.GetFirstImage()
56          while(image):
57             print "         Image"
58             print "         ---",image.GetDataEntry(0x0004,0x1500).GetString()
59             image=serie.GetNextImage()
60          serie=study.GetNextSerie()
61       study=patient.GetNextStudy()
62    patient=dicomdir.GetNextPatient()
63
64 dicomdir.SetDirectoryName(os.path.dirname(fileName))
65 dicomdir.Load()
66 if not dicomdir.IsReadable():
67    PrintUse()
68    raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName
69
70 print "DICOMDIR -->",fileName
71 print "##############################################################"
72 print "## Display all the elements and their respective values"
73 print "## found in the ", fileName, " file."
74 print "##############################################################"
75 dicomdir.SetPrintLevel(-1)
76 dicomdir.Print()
77