import gdcm
import os,sys
def TestScan(dirname, recursive = False):
system = gdcm.System()
if not system.FileIsDirectory(dirname):
print("Need a directory")
sys.exit(1)
d = gdcm.Directory()
nfiles = d.Load( dirname, recursive )
print("done retrieving all the",nfiles,"files")
s = gdcm.Scanner()
t1 = gdcm.Tag(0x0020,0x000d) t2 = gdcm.Tag(0x0020,0x000e) t3 = gdcm.Tag(0x0028,0x0011) t4 = gdcm.Tag(0x0028,0x0106) t5 = gdcm.Tag(0x0028,0x0107) 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)
values = s.GetValues()
print("Values found for all tags are:")
print(values)
mappings = s.GetMappings()
for f in d.GetFilenames():
print("Working on:",f)
mapping = s.GetMapping(f)
pttv = gdcm.PythonTagToValue(mapping)
pttv.Start()
while( not pttv.IsAtEnd() ):
tag = pttv.GetCurrentTag()
value = pttv.GetCurrentValue()
print(tag,"->",value)
pttv.Next()
if __name__ == "__main__":
try:
dirname = os.sys.argv[1]
recursive = True
except:
t = gdcm.Testing()
dirname = t.GetDataRoot()
recursive = False
TestScan( dirname, recursive)
sys.exit(0)