Skip to content

Instantly share code, notes, and snippets.

Last active June 23, 2018 16:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save colorbox/6ec0267f921a9aa92158bca513a610e9 to your computer and use it in GitHub Desktop.
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):
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