【发布时间】:2021-04-29 18:29:38
【问题描述】:
我正在用 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))
如何将imageBuffer_ptr 转换为shape=(h,w) 和dtype=c.ushort 的Numpy 数组?
我读到了这个答案:Convert Pointer to Array of Unsigned Chars to Numpy Array
这个答案:Getting data from ctypes array into numpy
两者都建议使用np.ctypeslib.as_array,但是当我尝试它时,我得到了这个错误:
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 说这是 Python 中的一个长期存在的错误(我使用的是 3.8)- 什么是解决方法?
【问题讨论】:
-
另外你是怎么调用函数的?你向它传递了什么论据?
-
@CristiFati 这是第 3 方 DLL;我没有来源,也不知道怎么称呼。