GDCM 3.2.1

CastConvertPhilips.py
1
14
15"""
16 Usage:
17
18 python --public /path/to/directory/
19 or
20 python --private /path/to/directory/
21
22 python --public --extension bak /path/to/directory/
23
24 rename -f 's/\.bak$//' *.bak
25
26 TODO:
27 http://docs.python.org/library/optparse.html#module-optparse
28"""
29
30 import vtkgdcm
31 import vtk
32 import sys
33 import gdcm
34
35 def ProcessOneFilePublic(filename, outfilename, tmpfile):
36 gdcm.ImageHelper.SetForceRescaleInterceptSlope(True)
37 vtkreader = vtkgdcm.vtkGDCMImageReader()
38 vtkreader.SetFileName( filename )
39 vtkreader.Update()
40
41 cast = vtk.vtkImageCast()
42 cast.SetInput( vtkreader.GetOutput() )
43 cast.SetOutputScalarTypeToUnsignedShort()
44
45 # vtkGDCMImageWriter does not support Sequence, so let's write a tmp file first:
46 # Some operation will actually be discarded (we simply need a temp storage)
47 vtkwriter = vtkgdcm.vtkGDCMImageWriter()
48 vtkwriter.SetFileName( tmpfile )
49 vtkwriter.SetMedicalImageProperties( vtkreader.GetMedicalImageProperties() )
50 vtkwriter.SetDirectionCosines( vtkreader.GetDirectionCosines() )
51 print "Format:",vtkreader.GetImageFormat()
52 vtkwriter.SetImageFormat( vtkreader.GetImageFormat() )
53 vtkwriter.SetInput( cast.GetOutput() )
54 #vtkwriter.Update()
55 vtkwriter.Write()
56
57 # ok now rewrite the exact same file as the original (keep all info)
58 # but use the Pixel Data Element from the written file
59 tmpreader = gdcm.ImageReader()
60 tmpreader.SetFileName( tmpfile )
61 if not tmpreader.Read():
62 sys.exit(1)
63
64 reader = gdcm.Reader()
65 reader.SetFileName( filename )
66 if not reader.Read():
67 sys.exit(1)
68
69 # Make sure to remove Slope/Rescale to avoid re-execution
70 ds = reader.GetFile().GetDataSet()
71 tags = [
72 gdcm.Tag(0x0028,0x1052),
73 gdcm.Tag(0x0028,0x1053),
74 gdcm.Tag(0x0028,0x1053),
75 ]
76 for tag in tags:
77 ds.Remove( tag )
78
79 writer = gdcm.ImageWriter()
80 writer.SetFileName( outfilename )
81 # Pass image from vtk written file
82 writer.SetImage( tmpreader.GetImage() )
83 # pass dataset from initial 'reader'
84 writer.SetFile( reader.GetFile() )
85 if not writer.Write():
86 sys.exit(1)
87
88 def ProcessOneFilePrivate(filename, outfilename, tmpfile):
89 vtkreader = vtkgdcm.vtkGDCMImageReader()
90 vtkreader.SetFileName( filename )
91 vtkreader.Update()
92
93
94 # (2005,1409) DS 4 0.0
95 # (2005,140a) DS 16 1.52283272283272
96
97 # (2005,0014) LO 26 Philips MR Imaging DD 005
98 tag1 = gdcm.PrivateTag(0x2005,0x09,"Philips MR Imaging DD 005")
99 tag2 = gdcm.PrivateTag(0x2005,0x0a,"Philips MR Imaging DD 005")
100
101
102
103 # Need to access some private tags, reread the file (for now):
104 reader = gdcm.Reader()
105 reader.SetFileName( filename )
106 if not reader.Read():
107 sys.exit(1)
108
109 ds = reader.GetFile().GetDataSet()
110
111 el1 = ds.GetDataElement( tag1 )
112 el2 = ds.GetDataElement( tag2 )
113
114
115 #pf = gdcm.PythonFilter()
116 #pf.SetFile( reader.GetFile() )
117 #print el1.GetTag()
118
119 print el1.GetByteValue()
120 v1 = eval(el1.GetByteValue().GetBuffer())
121 print el2.GetByteValue()
122 v2 = eval(el2.GetByteValue().GetBuffer())
123
124 print v1
125 shift = v1
126 print v2
127 scale = v2
128
129 ss = vtk.vtkImageShiftScale()
130 ss.SetInput( vtkreader.GetOutput() )
131 # because VTK image shift / scale convention is inverted from DICOM make sure shift is 0
132 assert shift == 0
133 ss.SetShift( shift )
134 ss.SetScale( scale )
135 ss.SetOutputScalarTypeToUnsignedShort ()
136 ss.Update()
137
138 # vtkGDCMImageWriter does not support Sequence, so let's write a tmp file first:
139 # Some operation will actually be discarded (we simply need a temp storage)
140 vtkwriter = vtkgdcm.vtkGDCMImageWriter()
141 vtkwriter.SetFileName( tmpfile )
142 vtkwriter.SetMedicalImageProperties( vtkreader.GetMedicalImageProperties() )
143 vtkwriter.SetDirectionCosines( vtkreader.GetDirectionCosines() )
144 vtkwriter.SetImageFormat( reader.GetImageFormat() )
145 # do not pass shift/scale again
146 vtkwriter.SetInput( ss.GetOutput() )
147 #vtkwriter.Update()
148 vtkwriter.Write()
149
150 # ok now rewrite the exact same file as the original (keep all info)
151 # but use the Pixel Data Element from the written file
152 tmpreader = gdcm.ImageReader()
153 tmpreader.SetFileName( tmpfile )
154 if not tmpreader.Read():
155 sys.exit(1)
156
157 writer = gdcm.ImageWriter()
158 writer.SetFileName( outfilename )
159 # Pass image from vtk written file
160 writer.SetImage( tmpreader.GetImage() )
161 # pass dataset from initial 'reader'
162 writer.SetFile( reader.GetFile() )
163 if not writer.Write():
164 sys.exit(1)
165
166 if __name__ == "__main__":
167
168 gdcm.Trace.DebugOff()
169 gdcm.Trace.WarningOff()
170 #filename = sys.argv[1]
171 #outfilename = sys.argv[2]
172 tmpfile = "/tmp/philips_rescaled.dcm"
173 #ProcessOneFile( filename, outfilename, tmpfile )
174 rescaletype = sys.argv[1]
175 assert rescaletype == "--public" or rescaletype == "--private"
176 dirname = sys.argv[2]
177 d = gdcm.Directory()
178 d.Load( dirname )
179
180 for f in d.GetFilenames():
181 #print f
182 ProcessOneFilePublic( f, f + ".bak", tmpfile )
183
184
185 print "success"
gdcm::Directory
Class for manipulation directories.
Definition gdcmDirectory.h:43
gdcm::ImageHelper::SetForceRescaleInterceptSlope
static void SetForceRescaleInterceptSlope(bool)
gdcm::ImageReader
ImageReader.
Definition gdcmImageReader.h:34
gdcm::ImageWriter
ImageWriter.
Definition gdcmImageWriter.h:33
gdcm::PrivateTag
Class to represent a Private DICOM Data Element (Attribute) Tag (Group, Element, Owner)
Definition gdcmPrivateTag.h:39
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::Trace::WarningOff
static void WarningOff()
gdcm::Trace::DebugOff
static void DebugOff()