SimpleITK  1.3.0.dev466
ImageIOSelection/ImageIOSelection.py
1 #!/usr/bin/env python
2 #=========================================================================
3 #
4 # Copyright Insight Software Consortium
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 #=========================================================================
19 
20 from __future__ import print_function
21 
22 import sys
23 import SimpleITK as sitk
24 
25 
26 if len(sys.argv)<2:
27  print('Wrong number of arguments.', file=sys.stderr)
28  print('Usage: ' + __file__ + ' image_file_name', file=sys.stderr)
29  sys.exit(1)
30 
31 # Find out which image IOs are supported
32 file_reader = sitk.ImageFileReader()
33 image_ios_tuple = file_reader.GetRegisteredImageIOs()
34 print("The supported image IOs are: " + str(image_ios_tuple))
35 print('-'*20)
36 
37 # Another option is to just print the reader and see which
38 # IOs are supported
39 print(file_reader)
40 print('-'*20)
41 
42 # Force the use of a specific IO. If the IO doesn't support
43 # reading the image type it will throw an exception.
44 file_reader.SetImageIO('PNGImageIO')
45 file_reader.SetFileName(sys.argv[1])
46 try:
47  image = file_reader.Execute()
48  print('Read image: ' + sys.argv[1])
49 except Exception as err:
50  print('Reading failed: ', err)
51  sys.exit(1)
52 
53 sys.exit(0)