import sys import os.path from gdcmConfigDemo import * from gdcmPython.core import * def PrintUse(): print "" print "Use :" print "-----" print "%s " % sys.argv[0] print " dicomDir : path to the DICOMDIR to parse" print "" print "" ### Get filename from command line or default it try: fileName = sys.argv[1] except IndexError: fileName = os.path.join(GDCM_DATA_ROOT, "DICOMDIR") if( not os.path.isfile(fileName) ): fileName=os.path.join(os.path.split(sys.argv[0])[0],"DICOMDIR") try: printLevel = int(sys.argv[2]) except IndexError: printLevel = 1 ### Build the DicomDir element list dicomdir = gdcm.DicomDir.New() dicomdir.SetFileName(fileName) dicomdir.Load() if not dicomdir.IsReadable(): PrintUse() raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName print "DICOMDIR -->",fileName print "##############################################################" print "## Display all the elements and their respective values" print "## found in the ", fileName, " file." print "##############################################################" dicomdir.SetPrintLevel(-1) dicomdir.Print() print "" patient=dicomdir.GetFirstPatient() while(patient): print "Patient" study=patient.GetFirstStudy() while(study): print " Study" serie=study.GetFirstSerie() while(serie): print " Serie" image=serie.GetFirstImage() while(image): print " Image" print " ---",image.GetDataEntry(0x0004,0x1500).GetString() image=serie.GetNextImage() serie=study.GetNextSerie() study=patient.GetNextStudy() patient=dicomdir.GetNextPatient() dicomdir.SetDirectoryName(os.path.dirname(fileName)) dicomdir.Load() if not dicomdir.IsReadable(): PrintUse() raise RuntimeError,"The '%s' DicomDir is not readable with gdcm." % fileName print "DICOMDIR -->",fileName print "##############################################################" print "## Display all the elements and their respective values" print "## found in the ", fileName, " file." print "##############################################################" dicomdir.SetPrintLevel(-1) dicomdir.Print()