Nov-30-2019, 07:59 PM
I created a post a little while back where I needed a function
'x' - a single array of rgb values
'y' - a 2d array full of rgb values
Here is the code for that:
I didn't notice this before (since my range of images was about 50 and it is now 1000 and over), but actually, the image created by my script, in in black and white.
Take these two arrays:
However, the way the code works is that it sums up all values and finds the closest matching one. That's where the problem lies:
although RGB set 2 is a different color to set 1, if they are summed up, they become the SAME VALUE. This means when they are compared to the 2d array of rgb values 'y', it will always return the same value even though it is a different colour.
As well as this, summing up rgb values is pretty much how you find the black and white version of an image.
The problem is all to do with the summing up of the values in the array. That means I need a way to compare 'x' to 'y' without (technically) doing
How can I do that?
Dream
findClosestArray(x, y)
which took two arguments:'x' - a single array of rgb values
[35,76,97]
'y' - a 2d array full of rgb values
[[33,76,90], [75, 97, 74], ...]
findClosestArray
would take x and find the closest matching array in 'y'. So for the arrays I posted before 'findClosestArray' would return [33,76,90]
, because that is closest to [35,76,97]
.Here is the code for that:
def find_closest(x, array2d): x_sum = sum(x) return min(array2d, key=lambda z: abs(sum(z) - x_sum))I have been using this every since, and have thought nothing of it because it worked perfectly. I have pretty much come to the end of my project - I am now in the stage of doing some last extensive testing.
I didn't notice this before (since my range of images was about 50 and it is now 1000 and over), but actually, the image created by my script, in in black and white.
Take these two arrays:
[1]: [50, 67, 98] - mucky blue [2]: [98, 67, 50] - browny-orange
. RGB set 2, is just the reverse of set 1. With the code I wanted, calling 'findClosestArray' should return a different value for each array, since they are TWO DIFFERENT COLOURS.However, the way the code works is that it sums up all values and finds the closest matching one. That's where the problem lies:
although RGB set 2 is a different color to set 1, if they are summed up, they become the SAME VALUE. This means when they are compared to the 2d array of rgb values 'y', it will always return the same value even though it is a different colour.
As well as this, summing up rgb values is pretty much how you find the black and white version of an image.
The problem is all to do with the summing up of the values in the array. That means I need a way to compare 'x' to 'y' without (technically) doing
fidClosestArray(sum(x), sum(y))
How can I do that?
Dream