diff options
| author | Chandler J <cjustice2000@gmail.com> | 2023-12-11 20:38:07 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-11 20:38:07 -0700 |
| commit | c11972edf3e08112d94083f0d3ccb7abec77df19 (patch) | |
| tree | 9fa7aef82b2fe2ad0f457acc7c821f14c1eef7d1 /src/color_engine.py | |
| parent | f029061dad768be5e7c39c1f6152660a0bbaf050 (diff) | |
| parent | c8f2a2635ec90499bd2ec75969f222f398c1bbc5 (diff) | |
Merge pull request #1 from chandlerj/refactor
Refactor
Diffstat (limited to 'src/color_engine.py')
| -rw-r--r-- | src/color_engine.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/color_engine.py b/src/color_engine.py new file mode 100644 index 0000000..f86b2e7 --- /dev/null +++ b/src/color_engine.py @@ -0,0 +1,49 @@ +import cv2 as cv +from sklearn.cluster import KMeans + +def grabColors(img_path: str, num_colors: int) -> list(): + """ + Takes in an image, and Number of colors, then returns a list of those colors. + The list of colors will contain the most prominent colors present in the image. + img_path - the path where your image lives (IE, /home/chandler/Pictures/moss.png) + num_colors - the number of colors you need back from the image + """ + img = cv.imread(img_path) + img = cv.cvtColor(img, cv.COLOR_BGR2RGB) + + # scale image down by factor of 10 to decrease computation time + dim = (int(len(img[0])/10), int(len(img)/10)) + img = cv.resize(img, dim, interpolation= cv.INTER_AREA) + clt = KMeans(n_clusters=num_colors, n_init='auto') + clt.fit(img.reshape(-1, 3)) + return clt.cluster_centers_ + +def rgbToHex(input_values: list): + """ + Takes in a list of RBG color values and returns a list of those same colors as hex values + """ + hex_list=[] + for color in input_values: + red = int(color[0]) + green = int(color[1]) + blue = int(color[2]) + hex_list.append('#{:02x}{:02x}{:02x}'.format(red, green, blue)) + return hex_list + +def hexToRGB(hex_value: str): + hex_value = hex_value.lstrip('#') + return tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4)) + +def compColors(color_list: list): + """ + given a list of colors, generate complimentary colors to contrast the prominent colors. + return a list of these colors. + """ + compliments = [] + for color in color_list: + curr_hex = color[1:] # slice off the # from the hex code + rgb = (curr_hex[0:2], curr_hex[2:4], curr_hex[4:6]) + comp = ['%02X' % (255 - int(a, 16)) for a in rgb] + compliments.append('#' + ''.join(comp)) + return compliments + |
