GDCM 3.2.1

ManipulateSequence.py
1
14
15"""
16 Usage:
17
18 python ManipulateSequence.py input.dcm output.dcm
19
20 This was tested using:
21
22 python ManipulateSequence.py gdcmData/D_CLUNIE_CT1_J2KI.dcm myoutput.dcm
23
24 This is a dummy example on how to modify a value set in a nested-nested dataset
25
26 WARNING:
27 Do not use as-is in production, this is just an example
28 This example works in an undefined length Item only (you need to explicitly recompute the length otherwise)
29"""
30
31 import sys
32 import gdcm
33
34 if __name__ == "__main__":
35
36 file1 = sys.argv[1]
37 file2 = sys.argv[2]
38
39 r = gdcm.Reader()
40 r.SetFileName( file1 )
41 if not r.Read():
42 sys.exit(1)
43
44 f = r.GetFile()
45 ds = f.GetDataSet()
46 tsis = gdcm.Tag(0x0008,0x2112) # SourceImageSequence
47 if ds.FindDataElement( tsis ):
48 sis = ds.GetDataElement( tsis )
49 #sqsis = sis.GetSequenceOfItems()
50 # GetValueAsSQ handle more cases
51 sqsis = sis.GetValueAsSQ()
52 if sqsis.GetNumberOfItems():
53 item1 = sqsis.GetItem(1)
54 nestedds = item1.GetNestedDataSet()
55 tprcs = gdcm.Tag(0x0040,0xa170) # PurposeOfReferenceCodeSequence
56 if nestedds.FindDataElement( tprcs ):
57 prcs = nestedds.GetDataElement( tprcs )
58 sqprcs = prcs.GetSequenceOfItems()
59 if sqprcs.GetNumberOfItems():
60 item2 = sqprcs.GetItem(1)
61 nestedds2 = item2.GetNestedDataSet()
62 # (0008,0104) LO [Uncompressed predecessor] # 24, 1 CodeMeaning
63 tcm = gdcm.Tag(0x0008,0x0104)
64 if nestedds2.FindDataElement( tcm ):
65 cm = nestedds2.GetDataElement( tcm )
66 mystr = "GDCM was here"
67 cm.SetByteStringValue( mystr )
68
69 w = gdcm.Writer()
70 w.SetFile( f )
71 w.SetFileName( file2 )
72 if not w.Write():
73 sys.exit(1)
gdcm::Reader
Reader ala DOM (Document Object Model)
Definition gdcmReader.h:54
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Writer
Writer ala DOM (Document Object Model)
Definition gdcmWriter.h:49