-
-
Notifications
You must be signed in to change notification settings - Fork 712
ENH: Implement PEP 688 buffer protocol for ITK Python #5673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: thewtex <[email protected]>
Co-authored-by: thewtex <[email protected]>
Co-authored-by: thewtex <[email protected]>
Co-authored-by: thewtex <[email protected]>
…king Co-authored-by: thewtex <[email protected]>
Co-authored-by: thewtex <[email protected]>
| # Get the memoryview from PyBuffer using the existing C++ method | ||
| # This returns a 1-D memoryview of the raw buffer | ||
| raw_memview = PyBufferType._GetArrayViewFromImage(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original PR, got a buffer from the import image container. This added a daisy chained reference to the object which actually holds the C buffer. This may help keep the Python buffer valid in some cases.
If this is not used, then perhaps the ImportImageContainer buffer interface should be removed. As I recall it was not properly wrapped for complex/composite pixel types.
Description
Implements PEP 688 buffer protocol support for ITK Images and ImportImageContainers, enabling zero-copy NumPy interoperability in Python 3.12+. Based on #5665 by @blowekamp with fixes for test failures and implementation refinements.
Key changes:
PyBuffer::_GetMemoryViewFromImportImageContainer()with overflow checking__buffer__()methods: Implemented forImageandImportImageContainerclassesPyBuffer._GetArrayViewFromImage()_get_formatstring()for robustnessImportImageContainer, CMake macro application__array__()uses buffer protocol for Python 3.12+, falls back toarray_from_image()otherwiseUsage:
PR Checklist
Related:
Co-authored-by: Bradley Lowekamp [email protected]
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.