-
Notifications
You must be signed in to change notification settings - Fork 0
/
Change Img size.py
44 lines (33 loc) · 990 Bytes
/
Change Img size.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
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy
import time
start_time = time.time()
img = plt.imread("ungthunao.png")
print(img.shape)
height = img.shape[0]
width = img.shape[1]
img = img.reshape(height * width, 3)
kmeans = KMeans(n_clusters=15, init="random").fit(img)
lables = kmeans.predict(img)
clusters = kmeans.cluster_centers_
# error = kmeans.inertia_
# print(error)
print(lables)
print(clusters)
img2 = numpy.zeros_like(img) # create a new img like "img"
for i in range(len(img2)):
img2[i] = clusters[lables[i]]
img2 = img2.reshape(height, width, 3)
# img2 = numpy.zeros((height, width, 3), dtype=numpy.uint8)
# index = 0
# for i in range(height):
# for j in range(width):
# lable_of_pixel = lables[index]
# img2[i][j] = clusters[lable_of_pixel]
# index += 1
end_time = time.time()
elapsed_time = end_time - start_time
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
plt.imshow(img2)
plt.show()