title: How can I create a circular mask for a numpy array?
body: <p>I am trying to circular mask an image in Python. I found some example code on the web, but I'm not sure how to change the maths to get my circle in the correct place.</p>
  
  <p>I have an image <code>image_data</code> of type <code>numpy.ndarray</code> with shape <code>(3725, 4797, 3)</code>:</p>
  
  <pre><code>total_rows, total_cols, total_layers = image_data.shape
  X, Y = np.ogrid[:total_rows, :total_cols]
  center_row, center_col = total_rows/2, total_cols/2
  dist_from_center = (X - total_rows)**2 + (Y - total_cols)**2
  radius = (total_rows/2)**2
  circular_mask = (dist_from_center &gt; radius)
  </code></pre>
  
  <p>I see that this code applies <em>euclidean distance</em> to calculate <code>dist_from_center</code>, but I don't understand the <code>X - total_rows</code> and <code>Y - total_cols</code> part. This produces a mask that is a quarter of a circle, centered on the top-left of the image.</p>
  
  <p>What role are <code>X</code> and <code>Y</code> playing on the circle? And how can I modify this code to produce a mask that is centered somewhere else in the image instead?</p>