SimpleITK  
ImageIOSelection/ImageIOSelection.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
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 import sys
21 import SimpleITK as sitk
22 
23 if len(sys.argv) < 2:
24  print("Wrong number of arguments.", file=sys.stderr)
25  print("Usage: " + __file__ + " image_file_name", file=sys.stderr)
26  sys.exit(1)
27 
28 # Find out which image IOs are supported
29 file_reader = sitk.ImageFileReader()
30 image_ios_tuple = file_reader.GetRegisteredImageIOs()
31 print("The supported image IOs are: " + str(image_ios_tuple))
32 print("-" * 20)
33 
34 # Another option is to just print the reader and see which
35 # IOs are supported
36 print(file_reader)
37 print("-" * 20)
38 
39 # Force the use of a specific IO. If the IO doesn't support
40 # reading the image type it will throw an exception.
41 file_reader.SetImageIO("PNGImageIO")
42 file_reader.SetFileName(sys.argv[1])
43 try:
44  image = file_reader.Execute()
45  print("Read image: " + sys.argv[1])
46 
47  size = image.GetSize()
48  print("Image size:", size[0], size[1])
49 except Exception as err:
50  print("Reading failed: ", err)
51  sys.exit(1)
52 
53 sys.exit(0)
itk::simple::ImageFileReader
Read an image file and return a SimpleITK Image.
Definition: sitkImageFileReader.h:60