title: What is the best way/method to digitize the data of a 3D surface into a grid of pixels with smaller resolution in Python?
body: <p>I want to digitize (= average out over cells) photon count data into pixels given by a grid that tells how they are aligned. The photon count data is stored in a 2D array. I want to split that data into cells, each of which would correspond to a pixel. The idea is basically the same as changing an HD image to a smaller resolution. I'd like to achieve this in Python.</p>
  <p>The digitizing function I've written:</p>
  <pre><code>import numpy as np
  
  def digitize(function_data, grid_shape):
      &quot;&quot;&quot;
      function_data = 2D array of function values of some 3D shape, 
      eg.: exp(-(x^2 + y^2 -&gt; want to digitize this
      grid_shape: an array of length 2 which contains the dimensions of the smaller resolution 
      &quot;&quot;&quot;
      l = len(function_data)
      pixel_len_x = int(l/grid_shape[0])
      pixel_len_y = int(l/grid_shape[1])
      digitized_data = np.empty((grid_shape[0], grid_shape[1]))
  
      for i in range(grid_shape[0]): #row-index of pixel in smaller-resolution grid
          for j in range(grid_shape[1]): #column-index of pixel in smaller-resolution grid
              hd_pixel = [] 
              for k in range(pixel_len_y):
                  hd_pixel.append(z_data[k][j:j*pixel_len_x])
              hd_pixel = np.ravel(hd_pixel) #turns 2D array into 1D to be able to compute average
              pixel_avg = np.average(hd_pixel)
              digitized_data[i][j] = pixel_avg
  
      return digitized_data 
  </code></pre>
  <p>In theory, this function should do what I want to achieve, but when tested it doesn't yield the expected results. Either a completed version of my function or any other method that achieves my goal would be extremely helpful.</p>