def histeq(path):
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
image = np.array(Image.open(path).convert('L'))
imhist,bins = np.histogram(image.flatten(),bins=255, normed=True)
cdf = imhist.cumsum()
cdf = 255 * cdf / cdf[-1]
im2 = np.interp(image.flatten(), bins[:-1], cdf).reshape(image.shape)
plt.figure(figsize=(12, 6))
plt.gray()
plt.subplot(221)
plt.hist(image.flatten(), 256)
plt.subplot(222)
plt.imshow(image)
plt.subplot(223)
plt.hist(im2.flatten(), 256)
plt.subplot(224)
plt.imshow(im2)
plt.show()
if __name__ == '__main__':
path = '../data/imgs/night.jpg'
histeq(path)