SimpleITK  
SimpleIO/SimpleIO.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
21
22"""
23These examples are used in the I/O documentation page. The IO.rst file
24pulls the code examples based their line numbers in this file. So any
25change in the line numbers of the code below will break the I/O page.
26"""
27
28import sys
29
30# A simple image input/output example
31#
32def example1(inputImageFileName, outputImageFileName):
33 """ A Simple Image Input/Output Example """
34
35 import SimpleITK as sitk
36
37 #START_OO_IMAGE_READER_WRITER_EXAMPLE
38 reader = sitk.ImageFileReader()
39 reader.SetImageIO("PNGImageIO")
40 reader.SetFileName(inputImageFileName)
41 image = reader.Execute()
42
43 writer = sitk.ImageFileWriter()
44 writer.SetFileName(outputImageFileName)
45 writer.Execute(image)
46 #END_OO_IMAGE_READER_WRITER_EXAMPLE
47
48
49# A simple procedural image input/output example
50#
51def example2(inputImageFileName, outputImageFileName):
52 """ A Simple Procedural Image Input/Output Example """
53
54 import SimpleITK as sitk
55
56 #START_PROCEDURAL_IMAGE_READER_WRITER_EXAMPLE
57 image = sitk.ReadImage(inputImageFileName, imageIO="PNGImageIO")
58 sitk.WriteImage(image, outputImageFileName)
59 #END_PROCEDURAL_IMAGE_READER_WRITER_EXAMPLE
60
61
62# A simple transform input/output example
63#
64def example3():
65 """ A Simple Transform Input/Output Example """
66
67 import SimpleITK as sitk
68
69 #START_PROCEDURAL_TRANSFORM_READER_WRITER_EXAMPLE
70 basic_transform = sitk.Euler2DTransform()
71 basic_transform.SetTranslation((2, 3))
72
73 file_name = "euler2D_py.tfm"
74 sitk.WriteTransform(basic_transform, file_name)
75 read_result = sitk.ReadTransform(file_name)
76
77 assert isinstance(read_result, sitk.Euler2DTransform)
78 #END_PROCEDURAL_TRANSFORM_READER_WRITER_EXAMPLE
79
80
81if __name__ == "__main__":
82
83 if len(sys.argv) < 3:
84 print("Usage: SimpleIO <input> <output>")
85 sys.exit(1)
86
87 example1(sys.argv[1], sys.argv[2])
88 example2(sys.argv[1], sys.argv[2])
89 example3()
90 sys.exit(0)
A rigid 2D transform with rotation in radians around a fixed center with translation.
Read an image file and return a SimpleITK Image.
Write out a SimpleITK image to the specified file location.
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)
SITKCommon_EXPORT Transform ReadTransform(const PathType &filename)