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 import sys
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 # A simple image input/output example
28 #
29 def example1(inputImageFileName, outputImageFileName):
30 
31  import SimpleITK as sitk
32 
33  reader = sitk.ImageFileReader()
34  reader.SetImageIO("PNGImageIO")
35  reader.SetFileName(inputImageFileName)
36  image = reader.Execute()
37 
38  writer = sitk.ImageFileWriter()
39  writer.SetFileName(outputImageFileName)
40  writer.Execute(image)
41 
42 
43 # A simple procedural image input/output example
44 #
45 def example2(inputImageFileName, outputImageFileName):
46 
47  import SimpleITK as sitk
48 
49  image = sitk.ReadImage(inputImageFileName, imageIO="PNGImageIO")
50  sitk.WriteImage(image, outputImageFileName)
51 
52 
53 # A simple transform input/output example
54 #
55 def example3():
56 
57  import SimpleITK as sitk
58 
59  basic_transform = sitk.Euler2DTransform()
60  basic_transform.SetTranslation((2, 3))
61 
62  sitk.WriteTransform(basic_transform, "euler2D.tfm")
63  read_result = sitk.ReadTransform("euler2D.tfm")
64 
65  assert type(read_result) == type(basic_transform)
66 
67 
68 if __name__ == "__main__":
69 
70  if len(sys.argv) < 3:
71  print("Usage: SimpleIO <input> <output>")
72  sys.exit(1)
73 
74  example1(sys.argv[1], sys.argv[2])
75  example2(sys.argv[1], sys.argv[2])
76  example3()
77  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:33
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