There are three ways:
Copy the image from the buffer to the NumPy array and vice versa. When a C / C ++ image is specified, create a NumPy array and copy the data; when a NumPy array is specified, create a C / C ++ image and copy the data.
The NumPy array as a wrapper around the data pointer C. Create a NumPy array, but with the specified data pointer. Make sure that the C / C ++ image containing the data pointer lives longer than the NumPy array. The NumPy array may live shorter, because in this case it will not try to delete the data.
C / C ++ image buffer as a wrapper around a pointer to a NumPy array. Create a NumPy array and retrieve the data pointer, and then use it to manupilate the NumPy array in place. Make sure the NumPy array lives long enough by holding the Python link.
Each of them can be implemented in a printed SWIG card.
In any case, you will need to convert the types of your types to NumPy types (NPY_UINT8, ..) and save the image size in npy_intp * .
To create a NumPy array, use PyArray_New or PyArray_SimpleNew or PyArray_SimpleNewFromData ( Array API ) and specify the necessary flags, such as Fortran-type adjacent or whatever is convenient for you. You can specify your own data pointer PyArray_New and PyArray_SimpleNewFromData .
The return value is PyObject* , which can be safely distinguished to PyArrayObject* (or run PyArray_Check earlier), and the data pointer is retrieved PyArray_DATA ( Array API ), which returns void * , which can then be applied to your desired type. You can then either make a copy or do the on-site modification.
To add a reference to a Python object, use Py_INCREF and Py_DECREF ( doc ) if you want to avoid the NumPy array for premature deletion.
If you want to be notified when a Python object is about to garbage, use a weak link through PyWeakref_NewRef ( doc ).
source share