Created
September 4, 2023 12:15
-
-
Save saswata-dutta/7ef860d10a9e72b84fb33b1cda937f46 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 numpy as np | |
from scipy.spatial import KDTree | |
rgb_list = np.array([(0, 255, 126), (255, 34, 121)]) # imagine 10k items | |
rgb_list_normalized = rgb_list / 255.0 | |
tree = KDTree(rgb_list_normalized) | |
def custom_distance(rgb1, rgb2): | |
return np.sum(np.abs(rgb1 - rgb2)) | |
query_rgb = np.array([128, 64, 32]) / 255.0 | |
distances, indices = tree.query(query_rgb, k=5, distance_upper_bound=custom_distance(query_rgb, rgb_list_normalized)) | |
nearest_rgb = rgb_list[indices] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment