title: Adjusting contrast of image purely with numpy
body: <p>I am trying write a contrast adjustment for images  in gray scale colors but couldn't find the right way to do it so far. This is what I came up with:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from PIL import Image
  from scipy import misc
  def fix_contrast(image):
      minimumColor = np.amin(image)
      maximumColor = np.amax(image)
      #avg = (minimumColor - maximumColor)/2 first attempt
      avg = np.mean(image) #second attempt
      colorDownMatrix = image &lt; avg # also tried
      colorUpMatrix = image &gt; avg 
      #also tried:   colorUpMatrix = image &gt; avg * 1.2
      # and : colorDownMatrix = image &lt; avg* 0.3
  
      image = image - minimumColor*colorDownMatrix
      image = image + maximumColor*colorUpMatrix
      lessThen0 = image&lt;0
      moreThen255 = image&gt;255
      image[lessThen0] = 0
      image[moreThen255] = 255
      return image    
  </code></pre>
  
  <p>My general attempt was to decrease elements towards 0 the pixels that their are "closer" to 0 and to increase elements towards 255 the elements that are "closer" to 255. I tried to measure closeness by mean function but before that by the arithmetic average, but all my attempts didn't get me to any good result.</p>
  
  <p>the image I am working on :
  <a href="https://i.stack.imgur.com/GO48R.png" rel="noreferrer"><img src="https://i.stack.imgur.com/GO48R.png" alt="enter image description here"></a></p>
  
  <p>the result I am aiming for:
  <a href="https://i.stack.imgur.com/asGvS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/asGvS.png" alt="goal picture"></a></p>
  
  <p>Am I anywhere close to the solution? Any hints/ tips will be great</p>