Getting data from ctypes array into numpy

前端 未结 6 803
执念已碎
执念已碎 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:31

    If you are ok with creating arrays in python, the following example with 2d array works in python3:

    import numpy as np
    import ctypes
    
    OutType = (ctypes.c_float * 4) * 6
    out = OutType()
    YourCfunction = ctypes.CDLL('./yourlib.so').voidreturningfunctionwithweirdname
    YourCfunction.argtypes = [ctypes.POINTER(ctypes.c_float)]*3, ctypes.POINTER(ctypes.c_float)]*5, OutType]
    YourCfunction(input1, input2, out)
    out = np.array(out) # convert it to numpy
    
    print(out)
    

    numpy and ctypes versions are 1.11.1 and 1.1.0

提交回复
热议问题