In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
In [2]:
im = Image.open('testim2.png').convert('L')
im = np.asarray(im.getdata()).reshape(im.size[::-1]).astype('uint8')
print(im.dtype, im.shape)
plt.imshow(im, cmap='gray')
Out[2]:
In [3]:
bins = np.bincount(im.ravel(), minlength=256)
acc = np.cumsum(bins/im.shape[0]/im.shape[1])
im2 = (acc[im] * 255).astype('uint8')
plt.imshow(im2, cmap='gray')
Out[3]:
In [4]:
plt.subplot(211)
_ = plt.hist(im.ravel(), 256, normed=1)
plt.subplot(212)
_ = plt.hist(im2.ravel(), 256, normed=1)