As a practice exercise, I'm trying to generate a color pallette from an image, and am using Python to turn the raw extracted RGB channels into a pallette. This is fairly simple using a combination of lists and dictionaries, but I'd like to be able to put a constraint the the number of colors in the pallette by combining similar color channels, unless they both have a large presence in the image.
Say I have a dictionary with my RGB values and their counts:
countsR = {"255": 301, "250": 16, "10": 589, "14": 124, "8": 132}
This pallette requires 3 bits to index into. Ideally I would run some function, combine_channels(dd, max_distance)
, which does some terrible O(N^2)
things, and outputs something like this:
print(combine_channels(countsR, 10))
>>> {"255": 317, "10": 845}
Which now may be indexed by only one bit!
It would also be good if it could keep track of which things it replaced with what, maybe in another dictionary:
countsR, replacements = combine_channels(countsR, 10)
print(replacements)
>>> {"255": ["250"], "10": ["8", "14"]}
Any ideas on what this may look like?
In many cases there's multiple different grouping options as the comments show. One easy option is to iterate through channels in numerical order forming groups and if a channel can't be fitted to existing group create a new one. It won't result to maximal distance but it will guarantee minimum number of groups to be generated:
Output:
Time complexity of above is O(n log n) since sorting is used.