GDCM  2.4.5
ManipulateSequence.py
1 ############################################################################
2 #
3 # Program: GDCM (Grassroots DICOM). A DICOM library
4 #
5 # Copyright (c) 2006-2011 Mathieu Malaterre
6 # All rights reserved.
7 # See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 #
9 # This software is distributed WITHOUT ANY WARRANTY; without even
10 # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the above copyright notice for more information.
12 #
13 ############################################################################
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.SetByteValue( mystr, gdcm.VL( len(mystr) ) )
68 
69  w = gdcm.Writer()
70  w.SetFile( f )
71  w.SetFileName( file2 )
72  if not w.Write():
73  sys.exit(1)

Generated on Fri Sep 25 2015 17:58:21 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo