ITK  4.9.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/IO/ImageBase/GenerateSlicesFromVolume/Code.py
1 #!/usr/bin/env python
2 
3 #==========================================================================
4 #
5 # Copyright Insight Software Consortium
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 #==========================================================================*/
20 
21 import sys
22 import itk
23 itk.auto_progress(2)
24 
25 if len(sys.argv) < 3:
26  print("Usage: " + sys.argv[0] + \
27  " <InputFileName> <OutputFileName> [Extension]")
28  sys.exit(1)
29 
30 inputFileName = sys.argv[1]
31 outputFileName = sys.argv[2]
32 if len(sys.argv) > 3:
33  extension = sys.argv[3]
34 else:
35  extension = ".png"
36 
37 fileNameFormat = outputFileName + "-%d" + extension
38 
39 Dimension = 3
40 
41 PixelType = itk.UC
42 InputImageType = itk.Image[PixelType, Dimension]
43 
44 ReaderType = itk.ImageFileReader[InputImageType]
45 reader = ReaderType.New()
46 reader.SetFileName(inputFileName)
47 
48 OutputPixelType = itk.UC
49 RescaleImageType = itk.Image[OutputPixelType, Dimension]
50 
51 RescaleFilterType = itk.RescaleIntensityImageFilter[InputImageType,
52  RescaleImageType]
53 rescale = RescaleFilterType.New()
54 rescale.SetInput(reader.GetOutput())
55 rescale.SetOutputMinimum(0)
56 rescale.SetOutputMaximum(255)
57 rescale.UpdateLargestPossibleRegion()
58 
59 region = reader.GetOutput().GetLargestPossibleRegion()
60 size = region.GetSize()
61 
63 fnames.SetStartIndex(0)
64 fnames.SetEndIndex(size[2] - 1)
65 fnames.SetIncrementIndex(1)
66 fnames.SetSeriesFormat(fileNameFormat)
67 
68 OutputImageType = itk.Image[OutputPixelType, 2]
69 
70 WriterType = itk.ImageSeriesWriter[RescaleImageType, OutputImageType]
71 writer = WriterType.New()
72 writer.SetInput(rescale.GetOutput())
73 writer.SetFileNames(fnames.GetFileNames())
74 
75 writer.Update()