0

I am trying to detect an exact pixel by its color, and then do something whether that pixel is being found or not. I think you (obviously) cannot make this with vanilla python, but I have not found any module in order to do this. If this is possible, I'd like to get some examples. Thanks in advice.

Edit: Basically, I need to scan a pixel (1920x1080 screen) and get the RGB Values

2
  • what do you mean "by its color"? like by specifying the RGB values? E.g., [255, 255, 255]? I think you can add more details to your question, it seems rather vague Commented May 15, 2020 at 2:46
  • Yeah, basically scanning a pixel and then gather its RGB values, from 0 to 255 Commented May 15, 2020 at 3:57

2 Answers 2

2

You can use numpy and pyautogui:

import numpy as np
import pyautogui

image = np.array(pyautogui.screenshot())

np.argwhere(image == [255, 255, 255])

It will return all points who are of the specified color.

Sign up to request clarification or add additional context in comments.

4 Comments

I assume 255, 255, 255 equals white but, that doesn't return anything
Maybe nothing is completely white? Looking around, I notice that few things, if anything, are completely white. You might have more luck with [0, 0, 0] but with all the possibilities (255**3), your chances are small. Maybe try an interval
I looped this with while True: but nothing shows up. I don't think this code is supossed to print anything on the console though.
Tbh I have no idea what you're doing. I haven't seen your code
1

You can use Pillow to do what you want.

You can try the following function. It's a modified version of the code from https://stackoverflow.com/a/765774/8581025.

from PIL import Image

def detect_color(rgb, filename):
    img = Image.open(filename)
    img = img.convert('RGBA')
    data = img.getdata()

    for item in data:
        if item[0] == rgb[0] and item[1] == rgb[1] and item[2] == rgb[2]:
            return True
    return False

For example, if you want to know if there is a red pixel (255, 0, 0) in example.png file in the current working directory, you can use the following code.

detect_color((255, 0, 0), 'example.png')  # returns True if there is a red pixel, False otherwise.

For getting a pixel color at specific coordinate, you can refer Get pixel's RGB using PIL.

3 Comments

Thank you. Really useful information. I see this would work for a png but, could this be done for the entire screen (let's say 1920x1080) with PIL?
Or maybe scan a single window with proccess name or pid
Maybe taking a snapshot of the entire screen can be done with Pillow (only on macOS and Windows). You can see the following links: stackoverflow.com/a/41384253/8581025, pillow.readthedocs.io/en/3.1.x/reference/ImageGrab.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.