-
Notifications
You must be signed in to change notification settings - Fork 17
/
Image_Compressor.py
95 lines (69 loc) · 2.5 KB
/
Image_Compressor.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import sys
from PIL import Image
import numpy as np
# A function that creates initial points for the centroids.
def initialize_K_centroids(X, K):
m = len(X)
return X[np.random.choice(m, K, replace=False), :]
# A function to find the closest centroid for each training example
def find_closest_centroids(X, centroids):
m = len(X)
c = np.zeros(m)
for i in range(m):
distances = np.linalg.norm(X[i] - centroids, axis=1)
c[i] = np.argmin(distances)
return c
# Compute the distance of each example to its centroid and take the average of distance for every centroid
def compute_means(X, idx, K):
_, n = X.shape
centroids = np.zeros((K, n))
for k in range(K):
examples = X[np.where(idx == k)]
mean = [np.mean(column) for column in examples.T]
centroids[k] = mean
return centroids
# Find K-means
def find_k_means(X, K, max_iters=10):
centroids = initialize_K_centroids(X, K)
previous_centroids = centroids
for _ in range(max_iters):
idx = find_closest_centroids(X, centroids)
centroids = compute_means(X, idx, K)
if (centroids == previous_centroids).all():
# The centroids aren't moving anymore.
return centroids
else:
previous_centroids = centroids
return centroids, idx
# Get input Image
image_path = "./test_goku.png"
# Load Image from the path and return as a Numpy array
def load_image(path):
image = Image.open(path)
return np.asarray(image) / 255
image = load_image(image_path)
w, h, d = image.shape
print(f"Image found with width: {w}, height: {h}, depth: {d}")
X = image.reshape((w * h, d))
K = 12 # The desired number of colors in the compressed image
# Get new colors with K-means
colors, _ = find_k_means(X, K, max_iters=20)
idx = find_closest_centroids(X, colors)
# Image Reconstruction
idx = np.array(idx, dtype=np.uint8)
X_reconstructed = np.array(colors[idx, :] * 255, dtype=np.uint8).reshape((w, h, d))
compressed_image = Image.fromarray(X_reconstructed)
compressed_image.save("out.png")
# Getting file stats
def convert_bytes(num):
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
def file_size(file_path):
if os.path.isfile(file_path):
file_info = os.stat(file_path)
return convert_bytes(file_info.st_size)
print(f"Original Image size is {file_size(image_path)}")
print(f"After Compression, the size is {file_size(r'out.png')}")