Return pixel of a certain color analyzed by Imagegrab

Asked

Viewed 384 times

1

I need to identify a color in a pixel "rectangle" and return its position to save to a variable. For example, in this image, I need to detect the blue color, and when detected it returns to me the X, Y position of it.

inserir a descrição da imagem aqui

I tried to create the following code in Python to accomplish this task, but it does not work. I searched many forums about, but I didn’t get any results. What I might be doing wrong ?

from tkinter import *

from PIL import ImageGrab
import pyautogui

running = False


def capture_screen():
    global screen
    screen = ImageGrab.grab()
    return screen


def scanning():
    if running:
        screen = capture_screen
        for x in range(1658, 1766):
            for y in range(31, 138):
                check_pixel = screen.getpixel((x, y))
                print("Check pixel", check_pixel)
                resolution = pyautogui.size()
                print(resolution)
    root.after(80, scanning)


def start():
    global running
    if running != True:
        running = True
    elif running == True:
        running = False


def stop():
    global running
    running = False


root = Tk()
root.title("Tstes")
root.geometry("100x100")

app = Frame(root)
app.grid()

start = Button(app, text="Start Scan", command=start)
stop = Button(app, text="Stop", command=stop)

start.grid()
stop.grid()

root.after(1, scanning)
root.mainloop()

1 answer

1


Murilo tries to follow this line:

from tkinter import *

from PIL import ImageGrab
import pyautogui

screen = ImageGrab.grab(bbox =(x1, y1, x2, y2)) 
screen.show()

Where x and y, are print position and size that Voce wants to capture

done this do the for to iterate under x and y equal you mounted in the example (Just need to change the range, to be equal to what Voce wants)

Inside the For loop just use:

check_pixel = screen.getpixel((x, y))
print("Check pixel", check_pixel)

Here Voce has to change the (255,255,255) to the blue rgb that Voce wants to capture, and if you find only play in a variable the x e y current

 if check_pixel ==   (255,255,255):
       print('a')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.