convert tripple pointer array to numpy array or list python
<p>I'm speeding up my program by integrating c code into my python program. I'm using <code>ctypes</code> to execute functions in c from python. </p>

<p>c program: </p>

<pre><code>    #include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;

#define MAX_ITERATIONS 1000

static void calculate_pixel(int x, int y, int size, float*** img){
    int i = 0;
    float a = 0.0, b = 0.0;

    while(a*a+b*b &lt;= 4.0 &amp;&amp; i &lt; MAX_ITERATIONS){
        float temp_a = a;
        a = a*a - b*b + x;
        b = 2*a*b + y;

        i++;
    }

    float temp[3];
    memset(&amp;temp, 0, sizeof(float)*3);

    if(i != MAX_ITERATIONS){
        float brightness = logf(1.75 + i - logf(log(a*a + b*b))) / log(MAX_ITERATIONS);
        temp[0] = brightness;
        temp[1] = brightness;
        temp[2] = 1;
    }

    memcpy(img[x][y], &amp;temp, sizeof(float)*3);
} 

float*** mandel(int size){
    ssize_t len = (size)*sizeof(float*);
    float*** img = malloc(len);

    int x, y;
    for(x = 0; x &lt; size; x++){
        img[x] = malloc(len);
        for(y = 0; y &lt; size; y++){
            img[x][y] = malloc(sizeof(float)*3);
            calculate_pixel(x, y, size, img);
        }
    }

    return img;
}
</code></pre>

<p>Python program:</p>

<pre><code> from ctypes import *
 import matplotlib.pyplot as pl

 size = 1000
 lib = './mandelbrot3.so'

 dll = cdll.LoadLibrary(lib)
 dll.mandel.argtypes = [c_int]

 #what i get in return from the c program
 dll.mandel.restype = POINTER(POINTER(POINTER(c_float*3)*size)*size)

 #calling function "mandel" in c program
 res = dll.mandel(size)

 #printing first value, does work this way
 print(res.contains[0].contains[0].contains[0])

 #creating a picture with pyplot, and inserting the array this way,
 #does not work because its pointers
 pl.imshow(res.contains)
 pl.show()
</code></pre>

<p><code>dll.mandel.restype</code>is a tripple pointer with size: 1000*1000*3. This is creates a picture where the size is 1000*1000 pixels and the 3 floats is the rgb values. </p>

<p>so my problem is that what i get back from the c program is just a tripple pointer. And i need to be able to convert it over to a normal 3d python list or numpy array. Is there a better way to do this than just reading all the elements in the pointer array with a for loop and inserting them in to the new list or numpy array?</p>

<p>I'm not sure about this triple-pointer business, but getting arrays from C to numpy works like this:</p>

<p>Assuming you have already received a ctypes <code>POINTER</code> to your image buffer, and you know the <code>height</code>, <code>width</code> and data type...</p>

<p>You can make a <code>ctypes</code> array the same size as your image from your buffer like so:</p>

<pre><code>arr = (c_float * height * width * 3).from_address(addressof(data.contents))
</code></pre>

<p>In this case <code>data</code> is the <code>POINTER</code>.</p>

<p>Then create an <code>ndarray</code> from it using the <code>buffer</code> kwarg:</p>

<pre><code>import numpy as np
img = np.ndarray(buffer=arr, dtype=np.float32, shape=(height, width, 3))
</code></pre>

python c arrays python-3.x ctypes