How does K-mean Clustering Work?
K-means partitions all data points into k number of clusters according to there "closeness" to the centroid assigned to the cluster. Then you take the averages of all of the points in each cluster to form a new centroid for that cluster. Rinse and repeat until there is no change in the centroids.
In this case, our data points is the RGB data of each point and we calculate the closeness by using the euclidean distance formula for 3d space. The position of the pixel in the image has nothing to do with how the pixels are grouped. You can see this rgb distance relationship in the graph
How do I calculate k?
There is not a real solution to this problem and that is basically the question when it comes to K-mean clustering. The easiest way to find a value is to just look at your favicon and count the number of groups of colors(so all the different shades of orange are grouped, etc).
You can also look at the graph and see where there are clumps of data. Another suggestion is to look at the color breakdown and see what colors are most prominent or have the most of that color. Ignore the outliers
What is this ever used for?
Believe it or not, this is how Google Chrome (ver. 15-17) calculates the color-stripe for the most visited page whenever you open up a new tab. Here are some examples: