From sklearn. datasets import load_sample_imageimport matplotlib. pyplot as pltfrom sklearn. cluster import kmeansimport numpy as npchina = load_sample_image ("china.jpg") PLT. imshow (China) PLT. show () image = China [: 3,: 3] # reduce the resolution PLT. imshow (image) PLT. show () x = image. reshape (-256,256,256) # recreate array n_colors = 64 # () model = kmeans (n_colors) #64 class clustering center labels = model. fit_predict (x) # color classification of each vertex, 0-63colors = model. cluster_centers _ #64 cluster center value new_image = colors [labels] # color filling new_image = new_image.reshape (image. shape) # restore the original array PLT. imshow (new_image.astype (NP. uint8) # convert to the Data Type and draw the PLT. show ()
# View image size import sysprint (SYS. getsizeof (China) # original image print (SYS. getsizeof (new_image) # New Image
819968128
# Save the original and new images as files to observe the file size.
# Understanding Bayesian theorem: # M Bucket: 7 Red 3 yellow # N Bucket: 1 red 9 yellow # now: a red ball is taken out # question: what is the probability that the red ball is M and N buckets?
Kmeans Image Compression