File size: 1,031 Bytes
c898ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
from matplotlib import image as mpimg
from sklearn.cluster import KMeans
import os

def compress_image(img,k):
    
    if img.max() <= 1:
        img = (img * 255).astype(np.uint8)

    # Check if the image is grayscale
    if len(img.shape) == 2:
        img = np.stack([img] * 3, axis=-1)  # Convert grayscale to RGB


    pixels = img.reshape(-1, 3)


    kmeans = KMeans(n_clusters=k, random_state=42, max_iter=1000)
    kmeans.fit(pixels)


    print("Cluster centers:\n", kmeans.cluster_centers_)
    print("Labels:\n", kmeans.labels_)

    # Assign the new colors to the pixels
    new_pixels = kmeans.cluster_centers_[kmeans.labels_]

    # Reshape back to the original image shape
    compressed_img = new_pixels.reshape(img.shape).astype(np.uint8)
    compressed_img_path = "compressed_image.png"
    mpimg.imsave(compressed_img_path, compressed_img)

    file_size_kb = os.path.getsize(compressed_img_path) / 1024
    return compressed_img,  round(file_size_kb, 2)