import numpy as np
from .models import GrayscaleImage

def shade_at_percentile(hist, percentile):
    """ Assumes the argument percentile,
        to be less 1.
    """
    n = np.sum(hist)
    cumulative_sum = np.cumsum(hist)
    
    return np.argmax(cumulative_sum/n >= percentile)

def auto_contrast(image):
    """ Apply auto contrast to an image using
        https://stackoverflow.com/questions/9744255/instagram-lux-effect/9761841#9761841
    """
    hist, _ = np.histogram(image.image.ravel(), bins=np.arange(0, 256))
    p5 = shade_at_percentile(hist, .01)
    p95 = shade_at_percentile(hist, .99)
    a = 255.0/(p95 + p5)
    b = -1.0 * a * p5

    result = (image.image.astype(float) * a) + b
    result = result.clip(0, 255.0)
    
    return GrayscaleImage(np.uint8(result), image.width, image.height)
@@ -525,17 +525,18 @@
 ))%0A    p
-5
+01
  = shade
@@ -567,17 +567,17 @@
 )%0A    p9
-5
+9
  = shade
@@ -623,14 +623,15 @@
 /(p9
-5
+9
  + p
-5
+01
 )%0A  
@@ -648,17 +648,18 @@
  * a * p
-5
+01
 %0A%0A    re

rename variable