diff --git a/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in b/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in index df545398f8a..4d81e2ae63a 100644 --- a/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in +++ b/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in @@ -20,7 +20,7 @@ if source: source.UpdateLargestPossibleRegion() - itksize = image.GetLargestPossibleRegion().GetSize() + itksize = image.GetBufferedRegion().GetSize() dim = len(itksize) shape = [int(itksize[idx]) for idx in range(dim)] diff --git a/Wrapping/Generators/Python/Tests/extras.py b/Wrapping/Generators/Python/Tests/extras.py index 5495167acfc..4fabe2d6a5d 100644 --- a/Wrapping/Generators/Python/Tests/extras.py +++ b/Wrapping/Generators/Python/Tests/extras.py @@ -224,6 +224,14 @@ def custom_callback(name, progress): diff = itk.comparison_image_filter(image.astype(itk.F), image_back.astype(itk.F)) assert np.sum(diff) == 0 + +largest_region = itk.ImageRegion[2]([0, 0], [1024, 1024]) +image.SetLargestPossibleRegion(largest_region) +image_dict = itk.dict_from_image(image) +image_back = itk.image_from_dict(image_dict) +diff = itk.comparison_image_filter(image.astype(itk.F), image_back.astype(itk.F)) +assert np.sum(diff) == 0 + mesh_dict = itk.dict_from_mesh(mesh) mesh_back = itk.mesh_from_dict(mesh_dict) diff --git a/Wrapping/Generators/Python/itk/support/extras.py b/Wrapping/Generators/Python/itk/support/extras.py index f09e5232649..683cc7073bb 100644 --- a/Wrapping/Generators/Python/itk/support/extras.py +++ b/Wrapping/Generators/Python/itk/support/extras.py @@ -789,12 +789,15 @@ def dict_from_image(image: "itkt.Image") -> Dict: pixel_arr = itk.array_from_image(image) imageType = wasm_type_from_image_type(image) + buffered_region = image.GetBufferedRegion() + bufferedRegion = { "index": tuple(buffered_region.GetIndex()), "size": tuple(buffered_region.GetSize())} return dict( imageType=imageType, name=image.GetObjectName(), origin=tuple(image.GetOrigin()), spacing=tuple(image.GetSpacing()), - size=tuple(image.GetBufferedRegion().GetSize()), + size=tuple(image.GetLargestPossibleRegion().GetSize()), + bufferedRegion=bufferedRegion, direction=np.asarray(image.GetDirection()), data=pixel_arr, ) @@ -808,10 +811,20 @@ def image_from_dict(image_dict: Dict) -> "itkt.Image": if image_dict["data"] is None: image = ImageType.New() image.SetRegions(image_dict["size"]) + if "bufferedRegion" in image_dict: + buffered_region = itk.ImageRegion[image.GetImageDimension()]() + buffered_region.SetIndex(image_dict["bufferedRegion"]["index"]) + buffered_region.SetSize(image_dict["bufferedRegion"]["size"]) + image.SetBufferedRegion(buffered_region) image.Allocate(True) else: image = itk.PyBuffer[ImageType].GetImageViewFromArray(image_dict["data"]) image.SetRegions(image_dict["size"]) + if "bufferedRegion" in image_dict: + buffered_region = itk.ImageRegion[image.GetImageDimension()]() + buffered_region.SetIndex(image_dict["bufferedRegion"]["index"]) + buffered_region.SetSize(image_dict["bufferedRegion"]["size"]) + image.SetBufferedRegion(buffered_region) image.SetOrigin(image_dict["origin"]) image.SetSpacing(image_dict["spacing"]) image.SetDirection(image_dict["direction"])