Skip to content

Conversation

@blowekamp
Copy link
Member

Enables view and conversions to numpy arrays with the following:

img = sitk.Image((2,5), sitk.sitkVectorUInt8, 3)
a_view = np.asarray(img, copy=False)
a_copy = np.array(img, copy=True)

References to the Python Image object are maintained.

  • Remove internal GetMemoryViewFromImage function
  • Add pixel_types module to Python
  • Add array interface and buffer protocol to Image class
  • Add a CPython ImageBuffer class to have pyBuffer interface

COMPATIBILITY:

Images of vector pixel type with only 1 component now return memory views and arrays of n+1 dimensions. ( Previously the 1 size component dimension was dropped )

@blowekamp blowekamp requested review from dave3d and zivy and removed request for zivy November 19, 2025 19:36
@blowekamp
Copy link
Member Author

@dave3d Is there a way to tell the spell checker to ignore a whole file? In the pre-commit config or else where?

@dave3d
Copy link
Member

dave3d commented Nov 19, 2025

@dave3d Is there a way to tell the spell checker to ignore a whole file? In the pre-commit config or else where?

You could disable spell checking within the file by putting in the comment: "// spell-check-disable"

https://github.com/SimpleITK/CommentSpellCheck?tab=readme-ov-file#disabling-spell-checking

or maybe adding the file name to the "exclude" line in the YAML file.

@blowekamp blowekamp force-pushed the add_pybuffer_image_buffer branch 4 times, most recently from d8184b5 to 0db7604 Compare November 20, 2025 16:35
Copy link
Member

@zivy zivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So Pythonic and makes the usage of views safer for users. Very nice work!

Some trivial spelling that can be ignored.

@blowekamp blowekamp marked this pull request as ready for review November 20, 2025 17:28
blowekamp and others added 4 commits November 20, 2025 13:52
The ImageBuffer class maintains a reference the the python Image
instance, which solves the dangling pointer for memory buffers with
the prior _GetMemoryViewFromImage method.
Allow for implicit view as numpy arrays as read-only buffers which
maintain a reference to the Python Image object.

Implement  PEP 688 for Python >= 3.12, by adding the __buffer__
method.
Useful functions to determine the class of pixel type. This is current
in a private module and may be implemented C++ in the future.

fix up for pixel types
The function was private and can be removed without deprecation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants