How to implement global contrast normalization in python?
there are multiple issues with the code:


You force the values in the image to be uint8 (8-bit integer). Since the values are floats they will be casted/rounded to either 0 or 1.
This will later be interpreted as image in black and the darkest form of gray (1 out of 255).
Once you have proper floats as values PIL or pillow can't handle the array (they only do images with values in [0, 255])


The first problem happened because you/numpy wants the array to be a uint8.
The normalize version will have floats.

You should have used:

X_prime = X.astype(float)


Here is a working version of the code:

import numpy
import scipy
import scipy.misc
from PIL import Image


def global_contrast_normalization(filename, s, lmda, epsilon):
    X = numpy.array(Image.open(filename))

    # replacement for the loop
    X_average = numpy.mean(X)
    print('Mean: ', X_average)
    X = X - X_average

    # `su` is here the mean, instead of the sum
    contrast = numpy.sqrt(lmda + numpy.mean(X**2))

    X = s * X / max(contrast, epsilon)

    # scipy can handle it
    scipy.misc.imsave('result.jpg', X)


global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)


PS: X_prime = X will make X_prime reference X. So changing X_prime will also change X.