c = ar.copy()
for i, code in enumerate(codes):
    c[(scipy.r_[scipy.where(vecs == i)]), :] = code
scipy.misc.imsave('clusters.png', c.reshape(*shape))
print('saved clustered image')
Python - Find dominant/most common color in an image