I'm writing a callback function in Python:
@ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, np.ctypeslib.ndpointer(dtype=ctypes.c_ushort))
def FastModeCallback(hCam, x, y, w, h, binx, biny, imageBuffer_ptr):
image = np.ctypeslib.as_array(imageBuffer_ptr, shape=(h, w))
How can I convert imageBuffer_ptr into a Numpy array of shape=(h,w) and dtype=c.ushort?
I read this answer: Convert Pointer to Array of Unsigned Chars to Numpy Array
and this answer: Getting data from ctypes array into numpy
Both advise using np.ctypeslib.as_array but when I try it I get this error:
Exception ignored on calling ctypes callback function: <function FastModeCallback at 0x00000224E2992DC0>
Traceback (most recent call last):
File "C:\Users\Dave\data\Code\Python\Cheese\atik.py", line 472, in FastModeCallback
image = np.ctypeslib.as_array(imageBuffer_ptr, shape=(h, w))
File "C:\Program_Files\anaconda3\lib\site-packages\numpy\ctypeslib.py", line 521, in as_array
return array(obj, copy=False)
ValueError: '<P' is not a valid PEP 3118 buffer format string
Google makes it sound like this is a long-standing bug in Python (I'm on 3.8) - what is a workaround?