Skip to content

Commit 0a458df

Browse files
committed
DOC: Add Python code for ResampleAScalarImage
1 parent 86f6213 commit 0a458df

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Filtering/ImageGrid/ResampleAScalarImage/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ install(TARGETS ResampleAScalarImage
1313
COMPONENT Runtime
1414
)
1515

16-
install(FILES Code.cxx CMakeLists.txt
16+
install(FILES Code.cxx CMakeLists.txt Code.py
1717
DESTINATION share/ITKExamples/Code/Filtering/ImageGrid/ResampleAScalarImage
1818
COMPONENT Code
1919
)
@@ -26,3 +26,13 @@ add_test(NAME ResampleAScalarImageTest
2626
160
2727
120
2828
)
29+
30+
if(ITK_WRAP_PYTHON)
31+
add_test(NAME ResampleAScalarImageTestPython
32+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
33+
${CMAKE_CURRENT_BINARY_DIR}/Gourds.png
34+
OutputPython.png
35+
160
36+
120
37+
)
38+
endif()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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) != 5:
21+
print("Usage: " + sys.argv[0] + " <input_filename> <output_filename> <size_x> <size_y>")
22+
sys.exit(1)
23+
24+
input_filename = sys.argv[1]
25+
output_filename = sys.argv[2]
26+
output_size = [int(sys.argv[3]), int(sys.argv[4])]
27+
28+
input_image = itk.imread(input_filename)
29+
30+
input_spacing = itk.spacing(input_image)
31+
input_size = itk.size(input_image)
32+
dimension = input_image.GetImageDimension()
33+
output_spacing = [input_spacing[dim] * input_size[dim] / output_size[dim] for
34+
dim in range(dimension)]
35+
36+
transform = itk.IdentityTransform[itk.D, dimension].New()
37+
38+
output_image = itk.resample_image_filter(input_image,
39+
size=output_size,
40+
output_spacing=output_spacing,
41+
output_origin=itk.origin(input_image),
42+
transform=transform
43+
)
44+
45+
itk.imwrite(output_image, output_filename)

src/Filtering/ImageGrid/ResampleAScalarImage/Documentation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ C++
3535
.. literalinclude:: Code.cxx
3636
:lines: 18-
3737

38+
Python
39+
......
40+
41+
.. literalinclude:: Code.py
42+
:language: python
43+
:lines: 1, 16-
3844

3945
Classes demonstrated
4046
--------------------

0 commit comments

Comments
 (0)