How to return an C array to Python?

試著忘記壹切 提交于 2019-11-29 16:00:01

What kind of array are you using? One way, which I find convenient, is to use numpy arrays, and modify the data in place. Numpy already has a lot of great operations for manipulating integer arrays, so this is handy if you're trying to add some additional functionality.

Step 1: link your C extension to numpy

on windows, this is something like

#include "C:\Python34/Lib/site-packages/numpy/core/include/numpy/arrayobject.h"

on osx it's something like

#include "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/include/numpy/arrayobject.h"

Step 2: grab the pointer to the data. This is surprisingly easy

int* my_data_to_modify;
if (PyArg_ParseTuple(args, "O", &numpy_tmp_array)){
        /* Point our data to the data in the numpy pixel array */
        my_data_to_modify = (int*) numpy_tmp_array->data;
}

... /* do interesting things with your data */

2D numpy array in C

When you work with data this way, you can allocate it as a 2d array, e.g.

np.random.randint( 0, 100, (100,2) )

or all zeros if you want a blank slate

But all C cares about is contiguous data, which means you can loop through it by the lenght of a "row" and modify it as if it were a 2D array

for example, if you were passing in colors in rgb form, e.g., a 100x3 array of them, you would consider

int num_colors = numpy_tmp_array2->dimensions[0]; /* This gives you the column length */
int band_size = numpy_tmp_array2->dimensions[1]; /* This gives you the row length */

for ( i=0; i < num_colors * band_size; i += band_size ){
    r = my_data[i];
    g = my_data[i+1];
    b = my_data[i+2];
}

To modify the data in place, just change a value in the data array. On the Python side, the numpy array will have the changed value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!