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""" A SimpleITK example demonstrating how to explicitly select a specific
21 IO for image reading. """
22
23import sys
24import SimpleITK as sitk
25
26if 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
32file_reader = sitk.ImageFileReader()
33image_ios_tuple = file_reader.GetRegisteredImageIOs()
34print("The supported image IOs are: " + str(image_ios_tuple))
35print("-" * 20)
36
37# Another option is to just print the reader and see which
38# IOs are supported
39print(file_reader)
40print("-" * 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.
44file_reader.SetImageIO("PNGImageIO")
45file_reader.SetFileName(sys.argv[1])
46try:
47 image = file_reader.Execute()
48 print("Read image: " + sys.argv[1])
49
50 size = image.GetSize()
51 print("Image size:", size[0], size[1])
52except IOError as err:
53 print("Reading failed: ", err)
54 sys.exit(1)
55
56sys.exit(0)
Read an image file and return a SimpleITK Image.