ITK  5.2.0
Insight Toolkit
SphinxExamples/src/IO/ImageBase/GenerateSlicesFromVolume/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
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 import sys
18 import itk
19 itk.auto_progress(2)
20 
21 if len(sys.argv) < 3:
22  print("Usage: " + sys.argv[0] +
23  " <InputFileName> <OutputFileName> [Extension]")
24  sys.exit(1)
25 
26 inputFileName = sys.argv[1]
27 outputFileName = sys.argv[2]
28 if len(sys.argv) > 3:
29  extension = sys.argv[3]
30 else:
31  extension = ".png"
32 
33 fileNameFormat = outputFileName + "-%d" + extension
34 
35 Dimension = 3
36 
37 PixelType = itk.UC
38 InputImageType = itk.Image[PixelType, Dimension]
39 
40 ReaderType = itk.ImageFileReader[InputImageType]
41 reader = ReaderType.New()
42 reader.SetFileName(inputFileName)
43 
44 OutputPixelType = itk.UC
45 RescaleImageType = itk.Image[OutputPixelType, Dimension]
46 
47 RescaleFilterType = itk.RescaleIntensityImageFilter[InputImageType,
48  RescaleImageType]
49 rescale = RescaleFilterType.New()
50 rescale.SetInput(reader.GetOutput())
51 rescale.SetOutputMinimum(0)
52 rescale.SetOutputMaximum(255)
53 rescale.UpdateLargestPossibleRegion()
54 
55 region = reader.GetOutput().GetLargestPossibleRegion()
56 size = region.GetSize()
57 
59 fnames.SetStartIndex(0)
60 fnames.SetEndIndex(size[2] - 1)
61 fnames.SetIncrementIndex(1)
62 fnames.SetSeriesFormat(fileNameFormat)
63 
64 OutputImageType = itk.Image[OutputPixelType, 2]
65 
66 WriterType = itk.ImageSeriesWriter[RescaleImageType, OutputImageType]
67 writer = WriterType.New()
68 writer.SetInput(rescale.GetOutput())
69 writer.SetFileNames(fnames.GetFileNames())
70 
71 writer.Update()
itk::ImageSeriesWriter
Writes image data to a series of data files.
Definition: itkImageSeriesWriter.h:85
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::NumericSeriesFileNames::New
static Pointer New()
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86