title: Most efficient way to convert a multidimensional numpy array to ctypes array
body: <p>Hello, I am using ctypes module in python to run some image processing C code from python, for the purpose of optimisation of my code, and reducing the execution time.</p>
  
  <p>
  For this purpose, I am reading an image into a numpy array and then applying 2D convolution to the image with a kernel, producing a filtered image. I want achieve the same in C, in order to save some execution time.</p>
  
  <p>So, the first part of the problem is converting the numpy image array, to ctype array, so that I can perform convolution in C. 
  Here is my C code which does nothing right now, but I need it just to access the function definition :</p>
  
  <pre><code>#import &lt;math.h&gt;
  
  void convolution(int *array,int *kernel, int array_height, int array_width,
                   int kernel_height, int kernel_width) {
      int i=0;
  </code></pre>
  
  <p>here is my python code, that adds a wrapper function for this C function :</p>
  
  <pre><code>_convolution_ = ctypes.cdll.LoadLibrary(working_directory + 'libconvolution.so')
  class two_dimensional_matrix_() : 
  
      def from_param(self,param) : 
          typename = type(param).__name__
          if hasattr(self,'from_'+typename) : 
              return getattr(self,'from_'+typename)(param)
          else : 
              raise TypeError('cant convert %s' %typename)
  
      #for a list
      def from_list(self,param) : 
  
          c_array = ((ctypes.c_int * len(param))*len(param[0]))()
          for i in range(len(param)) : 
              for j in range(len(param[i])) :
                  c_array[i][j] = ctypes.c_int(param[i][j])
          return c_array
  
      #for a tuple
      def from_tuple(self,param) : 
          return self.from_list(param)
  
      #for a numpy array
      def from_ndarray(self,param) : 
  
          c_array = ((ctypes.c_int * len(param))*len(param[0]))()
          for i in range(len(param)) : 
              for j in range(len(param[i])) : 
                  c_array[i][j] = ctypes.c_int(param[i][j])
          return c_array
  
  two_dimensional_matrix = two_dimensional_matrix_()
  _convolution_.convolution.argtypes = [
      two_dimensional_matrix, two_dimensional_matrix,
      ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int
  ]
  _convolution_.convolution.restypes = ctypes.c_void_p
  </code></pre>
  
  <p>Even though this code works perfectly, what I want to know is that is there a more efficient way to perform the conversion from a numpy array or a list to ctypes array? Because I am using C extensions in python to save execution time, I want this time to be as little as possible.</p>
  
  <p><b>EDIT</B> :
  <p>
  as suggested by Daniel, I used <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ascontiguousarray.html" rel="nofollow"><code>numpy.ascontiguousarray</code></a>, and it works well for 1D numpy array, here is what I did : </p>
  
  <pre><code>c_array = numpy.ascontiguousarray(array,dtype=numpy.uint32)
  </code></pre>
  
  <p>but when I try the similar method for 2D arrays, it does not work, here is what I tried :</p>
  
  <pre><code>c_array = numpy.ascontiguousarray(array,dtype=numpy.ndarray)
  </code></pre>
  
  <p>when I use this, python crashes. What am I doing wrong here?</p>