Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Modules/Bridge/NumPy/wrapping/PyBuffer.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
else:
imgview = itkPyBuffer@PyBufferTypes@._get_image_view_from_contiguous_array(ndarr, ndarr.shape[-1:0:-1], ndarr.shape[0])
else:
imgview = itkPyBuffer@PyBufferTypes@._get_image_view_from_contiguous_array(ndarr, ndarr.shape[::-1], 1)
if ndarr.flags['C_CONTIGUOUS']:
imgview = itkPyBuffer@PyBufferTypes@._get_image_view_from_contiguous_array(ndarr, ndarr.shape[::-1], 1)
else:
imgview = itkPyBuffer@PyBufferTypes@._get_image_view_from_contiguous_array(ndarr, ndarr.shape, 1)

# Keep a reference
imgview._SetBase(ndarr)
Expand Down
18 changes: 18 additions & 0 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,24 @@ def custom_callback(name, progress):
arr_fortran = arr.copy(order="F")
image = itk.GetImageViewFromArray(arr_fortran)
assert np.array_equal(arr_fortran.shape, image.shape)
# Test that image_from_array handles array.T correctly (F-contiguous arrays)
test_arr = np.empty((1, 2, 3))
image_from_arr = itk.image_from_array(test_arr)
image_from_transpose = itk.image_from_array(test_arr.T)
image_from_transpose_copy = itk.image_from_array(test_arr.T.copy())
assert np.array_equal(image_from_arr.shape, test_arr.shape), \
f"Expected shape {test_arr.shape}, got {image_from_arr.shape}"
assert np.array_equal(image_from_transpose.shape, test_arr.T.shape), \
f"Expected shape {test_arr.T.shape}, got {image_from_transpose.shape}"
assert np.array_equal(image_from_transpose_copy.shape, test_arr.T.shape), \
f"Expected shape {test_arr.T.shape}, got {image_from_transpose_copy.shape}"
# Also verify with image_view_from_array for consistency
image_view_from_arr = itk.image_view_from_array(test_arr)
image_view_from_transpose = itk.image_view_from_array(test_arr.T)
assert np.array_equal(image_view_from_arr.shape, test_arr.shape), \
f"Expected shape {test_arr.shape}, got {image_view_from_arr.shape}"
assert np.array_equal(image_view_from_transpose.shape, test_arr.T.shape), \
f"Expected shape {test_arr.T.shape}, got {image_view_from_transpose.shape}"
image = itk.image_from_array(arr, is_vector=True)
assert image.GetImageDimension() == 2
image = itk.GetImageViewFromArray(arr, is_vector=True)
Expand Down