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