Skip to content

Instantly share code, notes, and snippets.

@cinjon
Last active June 9, 2020 02:36
Show Gist options
  • Save cinjon/904b53e0e17ec3916dae572e832d6576 to your computer and use it in GitHub Desktop.
Save cinjon/904b53e0e17ec3916dae572e832d6576 to your computer and use it in GitHub Desktop.
H = 10
kmeans = make_clusters(eigenvectors, num_clusters)
W = 18
T = 128
CD = 8
CR = int(T // CD)
colors = []
labels = kmeans.labels_
for i in range(kmeans.cluster_centers_.shape[0]):
hexcolor = '%06X' % randint(0, 0xFFFFFF)
colors.append(np.array([int(hexcolor[i:i+2], 16) for i in (0, 2, 4)]))
img = np.zeros((H*CD, W*CR, 3))
for counter_down in range(CD):
for counter_right in range(CR):
for x in range(H):
for y in range(W):
xpos = counter_down*H + x
ypos = counter_right*W + y
v1 = CR*W*H*counter_down + W*H*counter_right + x*W + y
img[xpos, ypos] = colors[labels[v1]]
print(img.max(), img.min(), img.shape, img.dtype)
image = Image.fromarray(img.astype(np.uint8))
path = '/content/tmp-%dcluster.png' % len(colors)
image.save(path)
display.display(display.Image(path, height=600, width=600))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment