From 9f288790e9895964619272586f4352e63d57dac4 Mon Sep 17 00:00:00 2001 From: Siddhardha Nanda <99672439+SID-6921@users.noreply.github.com> Date: Fri, 23 Oct 2026 10:27:45 -0400 Subject: [PATCH] BUG: Keep 2D scalar images 2D in image_from_vtk_image The 2D-vs-3D trim for a trailing size-1 VTK z dimension only ran for vector pixels. A 2D scalar image round-tripped through vtk_image_from_image/image_from_vtk_image came back 3D with a spurious z extent of 1. Apply the same trim regardless of pixel type. --- Wrapping/Generators/Python/Tests/extras.py | 8 ++++++++ Wrapping/Generators/Python/itk/support/extras.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Wrapping/Generators/Python/Tests/extras.py b/Wrapping/Generators/Python/Tests/extras.py index 00cdc475661..91406421049 100644 --- a/Wrapping/Generators/Python/Tests/extras.py +++ b/Wrapping/Generators/Python/Tests/extras.py @@ -789,6 +789,14 @@ def assert_images_equal(base, test): itk_image = itk.image_from_vtk_image(vtk_image) assert_images_equal(input_image, itk_image) + # A 2D scalar image must round-trip back to 2D, not grow a spurious + # z dimension of size 1 (VTK images are always stored with 3 dimensions). + image = itk.image_from_array(np.random.rand(8, 12).astype(np.float32)) + vtk_image = itk.vtk_image_from_image(image) + image_round = itk.image_from_vtk_image(vtk_image) + assert image_round.GetImageDimension() == 2 + assert_images_equal(image, image_round) + except ImportError: print("vtk not imported. Skipping vtk conversion tests") pass diff --git a/Wrapping/Generators/Python/itk/support/extras.py b/Wrapping/Generators/Python/itk/support/extras.py index ffb7c4798e6..a731b0c08fe 100644 --- a/Wrapping/Generators/Python/itk/support/extras.py +++ b/Wrapping/Generators/Python/itk/support/extras.py @@ -753,7 +753,7 @@ def image_from_vtk_image(vtk_image: vtk.vtkImageData) -> itkt.ImageBase: number_of_components = point_data.GetScalars().GetNumberOfComponents() is_vector = number_of_components != 1 dims = list(vtk_image.GetDimensions()) - if is_vector and dims[-1] == 1: + if dims[-1] == 1: dims = dims[:-1] # 2D, not 3D dims.reverse() if is_vector: