title: Creating an image mask with a linear gradient
body: <p>I am creating a circular mask in python as follows:</p>
  
  <pre><code>import numpy as np
  def make_mask(image, radius, center=(0, 0)):
      r, c, d = image.shape
      y, x = np.ogrid[-center[0]:r-center[0], -center[1]:r-center[1]]
      mask = x*x + y*y &lt;= radius*radius
      array = np.zeros((r, c))
      array[mask] = 1
      return array
  </code></pre>
  
  <p>This returns a mask of shape (r, c). What I would like to do is have a weighted mask where the weight is 1 at the center of the image (given by the center parameter) and decreasing linearly towards the edge of the image. So, his should be an added weight calculated between 0 and 1 (0 not included) in the line. I was thinking this should be something like:</p>
  
  <pre><code>distance = (center[0] - x)**2 + (center[1] - y)**2
  # weigh it inversely to distance from center
  mask = (x*x + y*y) * 1.0/distance
  </code></pre>
  
  <p>However, this will result in divide by 0 and the mask would not be between 0 and 1 either. </p>