In [1]:
import imgfun
import matplotlib.pyplot as plt
import matplotlib.image as mpimg 
import numpy as np

from PIL import Image

%matplotlib inline

Show the original image


In [2]:
img = np.array(Image.open('obama.jpg'))
plt.imshow(img)


Out[2]:
<matplotlib.image.AxesImage at 0x1073bd090>

Now run K-means clustering on the pixels and set pixel colors to cluster centers


In [4]:
kimg = imgfun.GenKmeansImage(img, 5)  # takes a few seconds
plt.imshow(kimg)


Out[4]:
<matplotlib.image.AxesImage at 0x10760e110>

In [ ]: