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