21 if len(sys.argv) != 3:
22 print(
"Usage: " + sys.argv[0] +
" <input_image> <output_image>")
26 input_file_name = sys.argv[1]
27 output_file_name = sys.argv[2]
30 PixelType = itk.ctype(
"unsigned char")
32 itk_image = itk.imread(input_file_name, PixelType)
33 OriginalRegion = itk_image.GetLargestPossibleRegion()
34 OriginalSize = OriginalRegion.GetSize()
36 print(f
"The size of the ITK image data read from the input file = {OriginalSize}\n")
41 np_view_array = itk.GetArrayViewFromImage(itk_image, ttype=ImageType)
42 print(f
"The size of the NumPy array viewed from itk::Image = {np_view_array.shape}")
48 np_array = itk.GetArrayFromImage(itk_image, ttype=ImageType)
49 print(f
"The size of the NumPy array copied from itk::Image = {np_array.shape}")
52 itk_np = itk.GetImageFromArray(np.ascontiguousarray(np_array))
54 region = itk_np.GetLargestPossibleRegion()
55 size = region.GetSize()
56 print(f
"ITK image data size after convesion from NumPy = {size}\n")
58 itk.imwrite(itk_np, output_file_name)
67 np_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.int32)
68 itk_np_view_data = itk.image_view_from_array(np_data)
70 print(f
"ITK image data pixel value at [2,1] = {itk_np_view_data.GetPixel([2,1])}")
71 print(f
"NumPy array pixel value at [2,1] = {np_data[2,1]}")