Getting data from ctypes array into numpy

前端 未结 6 806
执念已碎
执念已碎 2020-11-27 02:53

I am using a Python (via ctypes) wrapped C library to run a series of computation. At different stages of the running, I want to get data into Python, and spec

6条回答
  •  醉话见心
    2020-11-27 03:20

    np.ctypeslib.as_array is all you need here.

    From an array:

     c_arr = (c_float * 8)()
     np.ctypeslib.as_array(c_arr)
    

    From a pointer

     c_arr = (c_float * 8)()
     ptr = ctypes.pointer(c_arr[0])
     np.ctypeslib.as_array(ptr, shape=(8,))
    

提交回复
热议问题