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