ITK  4.13.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Bridge/VtkGlue/ConvertAnRGBitkImageTovtkImageData/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 import vtk
24 
25 from distutils.version import StrictVersion as VS
26 if VS(itk.Version.GetITKVersion()) < VS("4.10.0"):
27  print("ITK 4.10.0 is required.")
28  sys.exit(1)
29 
30 if len(sys.argv) != 2:
31  print('Usage: ' + sys.argv[0] + ' <InputFileName>')
32  sys.exit(1)
33 imageFileName = sys.argv[1]
34 
35 Dimension = 2
36 PixelComponentType = itk.UC
37 PixelType = itk.RGBPixel[PixelComponentType]
38 ImageType = itk.Image[PixelType, Dimension]
39 
40 reader = itk.ImageFileReader[ImageType].New()
41 reader.SetFileName(imageFileName)
42 
43 itkToVtkFilter = itk.ImageToVTKImageFilter[ImageType].New()
44 itkToVtkFilter.SetInput(reader.GetOutput())
45 
46 itkToVtkFilter.Update()
47 myvtkImageData = itkToVtkFilter.GetOutput()
48 print(myvtkImageData)