Skip to content

Instantly share code, notes, and snippets.

@pravj
Last active December 24, 2018 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pravj/5537fe59a13f6bacbe3855eced767ed9 to your computer and use it in GitHub Desktop.
Save pravj/5537fe59a13f6bacbe3855eced767ed9 to your computer and use it in GitHub Desktop.
# Reference: https://code.likeagirl.io/finding-dominant-colour-on-an-image-b4e075f98097
from sklearn.cluster import KMeans
# word block colors (color palette)
block_colors = []
# change dimension to (width x height, color-channels)
screen = screen.reshape((screen.shape[0] * screen.shape[1], 3))
# Collect 8 major colors in the image using KMeans clustering
# It will include some background colors also having majority share
color_cluster = KMeans(n_clusters=8)
color_cluster.fit(screen)
# histogram representing area proportion for each color
# refers to other function, https://github.com/pravj/semantris-solver
hist = find_histogram(color_cluster)
# filter out persistent/background colors from game background
for i, rgb in enumerate(color_cluster.cluster_centers_):
if not is_blocks_background_color(*rgb, hist[i]):
block_colors.append(rgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment