ITK  4.6.0
Insight Segmentation and Registration Toolkit
IO/DicomSliceRead.py
1 #==========================================================================
2 #
3 # Copyright Insight Software Consortium
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #==========================================================================*/
18 
19 #
20 # Example on the use of ImageFileReader to reading a single slice (it will read
21 # DICOM or other format), rescale the intensities and save it in a different
22 # file format.
23 #
24 
25 import itk
26 import sys
27 
28 if len(sys.argv) < 3:
29  print('Usage: ' + sys.argv[0] + ' inputFile.dcm outputFile.png')
30  sys.exit(1)
31 
32 #
33 # Reads a 2D image in with signed short (16bits/pixel) pixel type
34 # and save it as unsigned char (8bits/pixel) pixel type
35 #
36 InputImageType = itk.Image.SS2
37 OutputImageType = itk.Image.UC2
38 
39 reader = itk.ImageFileReader[InputImageType].New()
40 writer = itk.ImageFileWriter[OutputImageType].New()
41 
42 filter = itk.RescaleIntensityImageFilter[InputImageType, OutputImageType].New()
43 filter.SetOutputMinimum( 0 )
44 filter.SetOutputMaximum(255)
45 
46 filter.SetInput( reader.GetOutput() )
47 writer.SetInput( filter.GetOutput() )
48 
49 reader.SetFileName( sys.argv[1] )
50 writer.SetFileName( sys.argv[2] )
51 
52 writer.Update()