ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Core/Common/SetPixelValueInOneImage/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import sys
18 import itk
19 
20 if len(sys.argv) != 2:
21  print('Usage: ' + sys.argv[0] + ' <OutputFileName>')
22  sys.exit(1)
23 
24 Dimension = 2
25 
26 PixelType = itk.UC
27 
28 OutputFileName = sys.argv[1]
29 
30 ImageType = itk.Image[PixelType, Dimension]
31 
32 region = itk.ImageRegion[Dimension]()
33 
34 start = itk.Index[Dimension]()
35 start.Fill(0)
36 
37 region.SetIndex(start)
38 
39 size = itk.Size[Dimension]()
40 size[0] = 200
41 size[1] = 300
42 
43 region.SetSize(size)
44 
45 image = ImageType.New()
46 image.SetRegions(region)
47 image.Allocate()
48 image.FillBuffer(itk.NumericTraits[PixelType].ZeroValue())
49 
50 pixelIndex = itk.Index[Dimension]()
51 
52 for r in range(50):
53  pixelIndex[0] = 4 * r
54  pixelIndex[1] = 4 * r
55 
56  image.SetPixel(pixelIndex, 128)
57 
58  pixelIndex[0] = 4 * r
59  pixelIndex[1] = 200 - 4 * r
60 
61  image.SetPixel(pixelIndex, 255)
62 
63 for r in range(25):
64  pixelIndex[0] = 8 * r
65  pixelIndex[1] = 200 + 4 * r
66 
67  image.SetPixel(pixelIndex, 128)
68 
69  pixelIndex[0] = 8 * r
70  pixelIndex[1] = 250
71 
72  image.SetPixel(pixelIndex, 180)
73 
74 pixelIndex[0] = 95
75 pixelIndex[1] = 150
76 
77 image.SetPixel(pixelIndex, 200)
78 
79 pixelIndex[0] = 100
80 pixelIndex[1] = 150
81 
82 image.SetPixel(pixelIndex, 200)
83 
84 pixelIndex[0] = 105
85 pixelIndex[1] = 150
86 
87 image.SetPixel(pixelIndex, 200)
88 
89 pixelIndex[0] = 100
90 pixelIndex[1] = 155
91 
92 image.SetPixel(pixelIndex, 200)
93 
94 pixelIndex[0] = 100
95 pixelIndex[1] = 145
96 
97 image.SetPixel(pixelIndex, 200)
98 
99 itk.imwrite(image, OutputFileName)
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86