Reputation: 53
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
Upvotes: 0
Views: 13298
Reputation: 36584
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.
Upvotes: 2
Reputation: 2312
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.
Upvotes: 1