G
G
Gigabait2021-06-16 10:25:26
Python
Gigabait, 2021-06-16 10:25:26

What is the best way to get the color of a pixel?

I'm using getpixel to get the color, but the problem is that getting the color of a pixel doesn't always show the same result. (Colors differ) result (colors of squares from getpixel ) : original getpixel

r, g, b = screenshot_data.getpixel((center_x, center_y))

Then I find the nearest color, there are 6 colors in colors. But due to the large spread of cyan / blue or pink / white colors, it assigns an erroneous color.
c = 0
    for color in colors:
        diff = 0
        for i in range(3):
            diff += abs(color[i]-pix[i])
        if diff < 70:
            return names[c]
        c += 1

I thought about the idea to make a range of colors / shades and check if the color from getpixel is in this range, then return the name of the color. But I did not find a ready range of RGB colors on the Internet.
Tried via pyautogui.locateOnScreen. He's point blank in the 57x57 region and can't figure out what it is.
for key, value in images.items():
        elem = pyautogui.locateOnScreen(value['path'], region=(x, y, x+CELL_SIZE, y+CELL_SIZE) )
        if elem:
            print('Meat found!', elem)
            draw.rectangle([(s[0], s[1]),(s[0]+CELL_SIZE,s[1]+CELL_SIZE)], width = 1, fill=value['color'], outline='red')


In short, I need to determine exactly which of the 6 colors (red, blue, purple, white, green, yellow) a cell belongs to. My problem is that getpixel does not perfectly determine the color of the cell. As a result, it turns out that when comparing the nearest colors, pale pink is defined as white, or dark blue as purple.

Do you have any ideas how to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question