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
Python: Remove numpy requirement, use buffer protocol instead #615
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
acd7a0b
to
da69908
Compare
I fixed the tests, |
This comment was marked as resolved.
This comment was marked as resolved.
Updated for much better This still needs to be cleaned up a bit and I'm not sure it is worth adding over just the standard buffer protocol support. |
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.
Thanks for working on this and bringing us closer to getting rid of the numpy dependency. I like the array_interface
approach but if I understand this correct, we might still have an API breakage issue, right? Do you know if there is something like an implicit casting on the Python side happening when someone would try to pass the returned object to a function that takes a numpy array as parameter?
This comment was marked as resolved.
This comment was marked as resolved.
Yes, this breaks the API. Sadly there is no implicit cast:
This still works when passing it to other libraries that handle buffer protocol or array interface internally, in some code it would need an explicit construction of a numpy array, PIL Image or similar though. |
This comment was marked as resolved.
This comment was marked as resolved.
b1a81d7
to
c45f74a
Compare
Partly taken from zxing-cpp#283 (comment) This removes the requirement of `read_barcode` / `write_barcode` to have Numpy installed and uses [buffer protocol](https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#buffer-protocol) instead. Numpy arrays can already be converted to a buffer, for PIL images we get the `__array_interface__` and use its values to create a memory view with the right shape. The `write_barcode` function now returns a buffer instead of a Numpy array. This buffer also supports `__array_interface__` for easy conversion to PIL images or Numpy arrays. * For PIL: `img = Image.fromarray(result, "L")` * For Numpy: `img = np.array(result)` fixes zxing-cpp#283 Co-authored-by: axxel <awagger@gmail.com>
c45f74a
to
dbc6850
Compare
I don't really understand Cython enough to do a proper test with it yet, so I removed it from the TODO. |
If there is no documentation to adjust, I think this is ready. |
I checked |
Will squash and merge once the CI checks are done. Vielen Dank Axel. |
Super, vielen Dank auch an dich, Axel! |
Partly taken from #283 (comment)
This removes the requirement of
read_barcode
/write_barcode
to have Numpy installed and uses buffer protocol instead.Numpy arrays can already be converted to a buffer, for PIL images we get the
__array_interface__
and use its values to create a memory view with the right shape.The
write_barcode
function now returns a buffer instead of a Numpy array. This buffer also supports__array_interface__
for easy conversion to PIL images or Numpy arrays.img = Image.fromarray(result, "L")
img = np.array(result)
fixes #283
Co-authored-by: axxel
TODO:
Reduce
fromZXAlgorithms.h
write_barcode
, returning a buffer is OK hereWriteResult
orpy::buffer
directly fromwrite_barcode