################################################################################
################################################################################
#   K-MEAN
################################################################################
################################################################################

from PIL import Image
import numpy as np, numpy.random as rd, numpy.linalg as alg
import matplotlib.pyplot as plt


################################################################################
#   IMPORTATION IMAGE
################################################################################

im=Image.open("Chateau_Sarran.jpg")
im.show()
tab=np.array(im,dtype=float)


################################################################################
#   K-MEAN
################################################################################


k=8

# INITIALISATION

# À modifier!
decrease=False
V=float('inf')

# Initialisation de la partition
S=[[] for i in range(k)]
L,C,H=tab.shape
for i in range(L):
    for j in range(C):
        tirage=rd.randint(0,k)
        S[tirage].append([i,j])

# Initialisation des centroïdes
centr=[0]*k
    

while decrease:
    
    # Calcul des centroïdes
    #
    # À compléter
    #

        
    # Mise à jour de la partition
    S=[[] for i in range(k)]
    #
    # À compléter
    #

    # Gestion du booléen decrease
    #
    # À compléter
    # 


################################################################################
#   AFFICHAGE IMAGE COMPRESSEE
################################################################################

res=np.array(tab)
for q in range(k):
    for coord in S[q]:
        i,j=coord
        res[i,j,:]=centr[q]

res=np.array(res,dtype='uint8')
im=Image.fromarray(res)
im.show()

