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

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
HellGoat77
  • 53
  • 1
  • 2
  • 7
  • 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 – Nicolas Gervais May 15 '20 at 02:46
  • Yeah, basically scanning a pixel and then gather its RGB values, from 0 to 255 – HellGoat77 May 15 '20 at 03:57

2 Answers2

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.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • I assume 255, 255, 255 equals white but, that doesn't return anything – HellGoat77 May 15 '20 at 14:57
  • 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 – Nicolas Gervais May 15 '20 at 15:04
  • 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. – HellGoat77 May 15 '20 at 15:56
  • Tbh I have no idea what you're doing. I haven't seen your code – Nicolas Gervais May 15 '20 at 16:14
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.

Gorisanson
  • 2,202
  • 1
  • 9
  • 25
  • 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? – HellGoat77 May 15 '20 at 04:42
  • Or maybe scan a single window with proccess name or pid – HellGoat77 May 15 '20 at 04:44
  • Maybe taking a snapshot of the entire screen can be done with Pillow (only on macOS and Windows). You can see the following links: https://stackoverflow.com/a/41384253/8581025, https://pillow.readthedocs.io/en/3.1.x/reference/ImageGrab.html – Gorisanson May 15 '20 at 06:16