def TestScan(dirname, recursive=False): # Check the dirname is indeed a directory system = gdcm.System() if not system.FileIsDirectory(dirname): print "Need a directory" sys.exit(1) # Retrieve all the files within that dir (recursively?) d = gdcm.Directory() nfiles = d.Load(dirname, recursive) print "done retrieving all the", nfiles, "files" s = gdcm.Scanner() t1 = gdcm.Tag(0x0020, 0x000d) # VR::UI t2 = gdcm.Tag(0x0020, 0x000e) # VR::UI t3 = gdcm.Tag(0x0028, 0x0011) # VR::US # Some fun tags, with dual VR: t4 = gdcm.Tag(0x0028, 0x0106) # VR::US_SS t5 = gdcm.Tag(0x0028, 0x0107) # VR::US_SS s.AddTag(t1) s.AddTag(t2) s.AddTag(t3) s.AddTag(t4) s.AddTag(t5) b = s.Scan(d.GetFilenames()) if not b: print "Scanner failed" sys.exit(1) # Raw Values found: values = s.GetValues() print "Values found for all tags are:" print values # get the main super-map : mappings = s.GetMappings() #file1 = d.GetFilenames()[0]; #print file1 #m1 = s.GetMapping( file1 ) #print m1 #print dir(m1) #for k,v in m1.iteritems(): # print "item", k,v # For each file get the value for tag t1: for f in d.GetFilenames(): print "Working on:", f mapping = s.GetMapping(f) pttv = gdcm.PythonTagToValue(mapping) # reset iterator to start position pttv.Start() # iterate until the end: while (not pttv.IsAtEnd()): # get current value for tag and associated value: # if tag was not found, then it was simply not added to the internal std::map # Warning value can be None tag = pttv.GetCurrentTag() value = pttv.GetCurrentValue() print tag, "->", value # increment iterator pttv.Next()
if __name__ == "__main__": try: filename = os.sys.argv[1] except: # failure print "Need a filename" sys.exit(1) # setup reader r = vtkgdcm.vtkGDCMThreadedImageReader() r.FileLowerLeftOn() #dir = vtkDirectory() dir = gdcm.Directory() # Did user pass in a directory: system = gdcm.System() if system.FileIsDirectory(filename): nfiles = dir.Load(filename) files = dir.GetFilenames() # Need to construct full path out of the simple filename fullpath = vtkStringArray() for file in files: fullpath.InsertNextValue(file) r.SetFileNames(fullpath) assert fullpath.GetNumberOfValues() # Need at least one file ExecuteInformation(r, fullpath.GetValue(0), fullpath.GetNumberOfValues()) r.AddObserver("ProgressEvent", PrintProgress) r.Update() #print r.GetOutput() #print vtkMultiThreader.GetGlobalDefaultNumberOfThreads()