r/pythonhelp • • Sep 17 '22

Active screen recording.

Hello! I am trying to get a script that will actively view a changing screen while searching for certain colors or textures. I am very open to doing my own research but thought to come here for ideas first. Ultimately what I'd like Is a script that locates the "texture" some time multiples will be on screen, brings the mouse over it and interacts with a key press. Any suggestions?

2 Upvotes

2 comments sorted by

1

u/AmongstYou666 Sep 17 '22

import time
import numpy as np
import mss, cv2, os, sys
import pygame 
import pyautogui as py
from Xlib.display import Display
with mss.mss() as sct:
    monitor = sct.monitors[1]
pygame.init()
info = pygame.display.Info()
max_w = info.current_w*2
max_h = info.current_h
_display = Display(os.environ['DISPLAY'])
def colorRange(x, y):
    RGB = []
    RGB.append(myGetPixel(x,y))
    py.moveTo(x+20, y+20, 1)
    RGB.append(myGetPixel(x,y))
    matchThis = np.array(RGB)
    RGBrange = np.sort(matchThis.T)
    print(RGBrange, x, y)
    return(RGBrange)
def colorMatch(AR):
    TFresult = True
    for i in range(3):
        TFresult = TFresult and (AR[i] in range(RGBrange[i][0],RGBrange[i][1]))
    return(TFresult) 
   
def mousexy():    
    coord = _display.screen().root.query_pointer()._data
    #print ('Running... \ncoords {}'.format(coord))
    x = coord['root_x']
    y = coord['root_y']           
    return (x, y)
def passArray(AR = []): #pass array of variables
    print(len(AR), AR)
def myGetPixel(x,y):
    bbox = (x, y, x+1, y+1)
    img = np.array(sct.grab(bbox))
    np.array(img)
    #return RGB
    return ([img[0][0][0],img[0][0][1],img[0][0][2]])

1

u/AmongstYou666 Sep 17 '22

Some old code that I've used for something similar but needs to be updated to use pynput library