title: Change contrast in Numpy
body: <p>I want to write a pure Numpy function to change the contrast of an RGB image (that is represented as a Numpy uint8 array), however, the function I wrote doesn't work and I don't understand why.</p>
  
  <p>Here is an example image:</p>
  
  <p><a href="https://i.stack.imgur.com/Ceppy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ceppy.png" alt="enter image description here"></a></p>
  
  <p>And here is a function that uses PIL and works fine:</p>
  
  <pre><code>def change_contrast(img, factor):
      def contrast(pixel):
          return 128 + factor * (pixel - 128)
      return img.point(contrast)
  
  from PIL import Image
  
  img = Image.fromarray(img.astype(np.uint8))
  img1 = change_contrast(img, factor=2.0)
  </code></pre>
  
  <p>Output:</p>
  
  <p><a href="https://i.stack.imgur.com/cQM7N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cQM7N.png" alt="enter image description here"></a></p>
  
  <p>Now here is a pure Numpy function, which, in my opinion, does the exact same thing as the other function above, but it doesn't work at all:</p>
  
  <pre><code>def change_contrast2(img, factor):
      return 128 + factor * (img - 128)
  
  img1 = change_contrast2(img, factor=2.0)
  </code></pre>
  
  <p>where <code>img</code> is a Numpy array. The output is this:</p>
  
  <p><a href="https://i.stack.imgur.com/17SXU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/17SXU.png" alt="enter image description here"></a></p>
  
  <p>I don't understand what's going on and would be happy about any hints!</p>