GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

ReWriteSCAsMR.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 GDCM 1.x would write out MR Image Storage as Secondary Capture Object while still setting Rescale Slope/Intercept
17 and saving the Pixel Spacing in (0028,0030)
18 """
19
20 import gdcm
21 import sys,os
22
23 def CheckSecondaryCaptureObjectIsMRImageStorage(r):
24 ds = r.GetFile().GetDataSet()
25 # Check Source Image Sequence
26 if ds.FindDataElement( gdcm.Tag(0x0008,0x2112) ):
27 sis = ds.GetDataElement( gdcm.Tag(0x0008,0x2112) )
28 sqsis = sis.GetSequenceOfItems()
29 if sqsis.GetNumberOfItems():
30 item1 = sqsis.GetItem(1)
31 nestedds = item1.GetNestedDataSet()
32 if nestedds.FindDataElement( gdcm.Tag(0x0008,0x1150) ):
33 ReferencedSOPClassUID = nestedds.GetDataElement( gdcm.Tag(0x0008,0x1150) )
34 raw = ReferencedSOPClassUID.GetByteValue().GetPointer()
35 uids = gdcm.UIDs()
36 # what is the actual object we are looking at ?
37 ms = gdcm.MediaStorage()
38 ms.SetFromDataSet(ds)
39 msuid = ms.GetString()
40 uids.SetFromUID( msuid )
41 msuidname = uids.GetName() # real Media Storage Name
42 uids.SetFromUID( raw )
43 sqmsuidname = uids.GetName() # Source Image Sequence Media Storage Name
44 # If object is SC and Source derivation is MRImageStorage then we can assume 'Pixel Spacing' is correct
45 if( sqmsuidname == 'MR Image Storage' and msuidname == 'Secondary Capture Image Storage' ):
46 return True
47 # in all other case simply return the currentspacing:
48 return False
49
50 if __name__ == "__main__":
51 r = gdcm.ImageReader()
52 filename = sys.argv[1]
53 r.SetFileName( filename )
54 if not r.Read():
55 sys.exit(1)
56 f = r.GetFile()
57
58 if( CheckSecondaryCaptureObjectIsMRImageStorage(r) ):
59 # Special handling of the spacing:
60 # GDCM 1.2.0 would not rewrite correcly DICOM Object and would always set them as 'Secondary Capture Image Storage'
61 # while we would rather have 'MR Image Storage'
62 gdcm.ImageHelper.SetForcePixelSpacing( True )
63 mrspacing = gdcm.ImageHelper.GetSpacingValue( r.GetFile() )
64 # TODO: I cannot do simply the following:
65 #image.SetSpacing( mrspacing )
66 image.SetSpacing(0, mrspacing[0] )
67 image.SetSpacing(1, mrspacing[1] )
68 image.SetSpacing(2, mrspacing[2] )
69 gdcm.ImageHelper.SetForceRescaleInterceptSlope( True )
70 ris = gdcm.ImageHelper.GetRescaleInterceptSlopeValue( r.GetFile() )
71 image.SetIntercept( ris[0] )
72 image.SetSlope( ris[1] )
73
74 outfilename = sys.argv[2]
75 w = gdcm.ImageWriter()
76 w.SetFileName( outfilename )
77 w.SetFile( r.GetFile() )
78 w.SetImage( image )
79 if not w.Write():
80 sys.exit(1)
81
82 sys.exit(0)