Last active
June 23, 2018 16:29
-
-
Save colorbox/6ec0267f921a9aa92158bca513a610e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import numpy as np | |
from PIL import Image | |
from skimage import data | |
from sklearn.cluster import KMeans | |
cluster_count = 4 | |
feature = np.array([data.imread("./icons/" + path) for path in os.listdir('./icons')]) | |
feature = feature.reshape(len(feature), -1).astype(np.float64) | |
print('data reshape completed') | |
model = KMeans(n_clusters=cluster_count).fit(feature) | |
print('clustering completed') | |
labels = model.labels_ | |
for label, path in zip(labels, os.listdir('./icons')): | |
dist_dir_name = "icon_groups" | |
dirpath = dist_dir_name + "/" + str(label) | |
if not os.path.isdir(dirpath): | |
os.makedirs(dirpath) | |
shutil.copyfile("./icons/" + path, dist_dir_name + "/" + str(label) + "/" + path) | |
print(label, path) | |
print('all completed') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment