2

I have been working on this script that clicks the wherever the pixel of a certain color is on the screen, but i ran into a problem, when i loop this it takes screenshots, but only about 12 during a 30 second loop when it SHOULD be taking 600 for the program to click on the pixels extremely quickly. im lost

xx = 0
while xx <= 600:
    with mss.mss() as sct:
        region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
        imgg = sct.grab(region)
        img1 = mss.tools.to_png(imgg.rgb,imgg.size,output="C:/Users/yaahy/Desktop/scrnshotoutput/imageforgamehack"+str(xx)+".png")
        imgname = "C:/Users/yaahy/Desktop/scrnshotoutput/imageforgamehack"+str(xx)+".png"
    pxls = find_yellow_pixels(imgname)
    pyautogui.click(pxls[0],pxls[1])
    time.sleep(.05)
    xx = xx + 1
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • is it when grabbing the region, writing a file, or finding the pixel that your program is taking too long? you should profile your code to figure out where you need to improve it – Michael Speer Feb 21 '19 at 04:32
  • even something as simple as `start = datetime.datetime.now()`, then calling your function, then calling `stop = datetime.datetime.now()` and `print( stop - start )` will give you some good information to start with – Michael Speer Feb 21 '19 at 04:34
  • @MichaelSpeer the screenshots are saved slowly, usually taking like 4 seconds per screenshot leaving me with around 12 shots – Jake Sanford Feb 21 '19 at 04:34
  • Is it the sct.grab(...) or the mss.tools.to_png(...)? if it's in the latter, you might find it easier to avoid calling a png encoder and writing a file out to disk, instead looking through your image in RAM directly. Is there a reason you're writing to disk and then reading back off it to check for the data? – Michael Speer Feb 21 '19 at 04:38
  • Also, I hope this is for a single player game. It wouldn't be very kind to hack a multiplayer game, ruining the experience for others :) – Michael Speer Feb 21 '19 at 04:40
  • Im sorry, i am new to python, not too sure what you mean by this, i dont need it to save the files to my pc but its the only way ive gotten it to work so far.. and i thinks its the to_png since i have tried pyautogui to take screenshots and it was still just as slow – Jake Sanford Feb 21 '19 at 04:41
  • http://faculty.washington.edu/chudler/java/dottime.html is the game dont worry, just trying to expand my knowledge – Jake Sanford Feb 21 '19 at 04:42
  • @MichaelSpeer at'ing so you know i responded – Jake Sanford Feb 21 '19 at 04:44
  • 3
    instead of saving as a png and reloading, check into the `imgg.pixels` array. that way you avoid running a compression algorithm, waiting on disk to save, waiting on disk to load, and running a decompression algorithm in order to look at the pixels you grabbed – Michael Speer Feb 21 '19 at 04:55
  • could i make in image out of .pixels that doesnt save to my pc? that would be easiest @MichaelSpeer – Jake Sanford Feb 21 '19 at 04:58
  • 3
    imgg is already an object representing an image. `imgg.brga` has binary image information, and `imgg.pixels` has tuples of RGB colors grouped in tuples representing rows of data. so if you know what you're looking for, you can walk down the row tuples checking for columns that have the color-tuple you want. hopefully that makes sense – Michael Speer Feb 21 '19 at 05:04

1 Answers1

1

First, you should rewrite to not instanciate a new MSS class every iteration, and remove the sleep:

import mss

with mss.mss() as sct:
    region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}

    for xx in range(600):
        imgname = "C:/Users/yaahy/Desktop/scrnshotoutput/imageforgamehack" + str(xx) + ".png"
        imgg = sct.grab(region)
        img1 = mss.tools.to_png(imgg.rgb, imgg.size, output=imgname)
        # pxls = find_yellow_pixels(imgname)
        # pyautogui.click(pxls[0],pxls[1])

Then, as commentors suggested, you should explain what you want to achieve, perhaps you can get rid of the PNG creation and work directly with raw data.

Tiger-222
  • 6,677
  • 3
  • 47
  • 60