dustyroo

Improved

Apr 18th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. import mss
  2. import numpy as np
  3. from time import sleep, strftime, time
  4. from random import uniform, randint
  5. from keyboard import wait, send, add_hotkey, on_press, is_pressed, press as kpress, release as krelease
  6. from mouse import get_position, move, press, release, wheel
  7. from cv2 import cvtColor, COLOR_BGR2GRAY, matchTemplate, TM_CCOEFF_NORMED, minMaxLoc, imread
  8.  
  9. def humanMove(numberX, numberY, mincount, maxcount, minmove, maxmove):
  10.     """ Humanize moves the mouse cursor to a point on the screen. """
  11.     x, y = get_position()
  12.     randomNumber = randint(mincount, maxcount)
  13.     x2, y2 = (numberX - x) / randomNumber, (numberY - y) / randomNumber
  14.  
  15.     for number in range(0, number):
  16.         moveSpeed = uniform(0.04, 0.09)
  17.         offset = randint(minmove, maxmove)
  18.         move(x2 + offset, y2 + offset, False,moveSpeed)
  19.  
  20.     move(numberX, numberY,duration=moveSpeed)
  21.  
  22. def humanMoverel(numberX, numberY, mincount, maxcount, minmove, maxmove):
  23.     """ Humanize moves the mouse cursor to a point on the screen, relative to its current
  24.    position. """
  25.     randomNumber = randint(mincount, maxcount)
  26.     x, y = numberX / randomNumber, numberY / randomNumber
  27.  
  28.     for number in range(0, number):  
  29.         moveSpeed = uniform(0.04, 0.09)
  30.         offset = randint(minmove, maxmove)
  31.  
  32.         move(x + offset, y, False,duration=moveSpeed)
  33.  
  34. def humanClick(action):
  35.     """ Sends a humanize click with the given button. """
  36.     time_to_wait = uniform(0.047, 0.378)
  37.     press(action)
  38.     sleep(time_to_wait)
  39.     release(action)
  40.  
  41. def humanKeyp(key):
  42.     """ Humanize presses a given key. """
  43.     time_to_wait = uniform(0.047, 0.378)
  44.     kpress(key)
  45.     sleep(time_to_wait)
  46.     krelease(key)
  47.    
  48. def grabPixel(numberX, numberY):
  49.     """ Grabs a pixel colour on a point of the screen."""
  50.     with mss.mss() as sct:
  51.         try:
  52.             sct_img = sct.grab(sct.monitors[1])
  53.  
  54.             return sct_img.pixel(numberX, numberY)
  55.         except:
  56.             mon = sct.monitors[2]
  57.             sct_img = sct.grab(mon)
  58.  
  59.             return sct_img.pixel(numberX-mon["left"], numberY)
  60.  
  61. def pixelMatchesColor(colour, match, tolerance=0):
  62.     """Matches a pixel colour on a point of the screen."""
  63.     # should always be 3 or 4 (rbg, possibly with an alpha channel)
  64.     if colour == match:#exact match
  65.         return True
  66.     has_matched = True
  67.     if tolerance > 0:
  68.         for index in range(len(colour)):# make sure all 3 match
  69.             if colour[index]-tolerance > match[index]:# too high
  70.                 has_matched = False
  71.             elif colour[index]+tolerance < match[index]:# too low
  72.                 has_matched = False
  73.     if has_matched == True:
  74.         return True
  75.     else:
  76.         return False
  77.    
  78. def pixelFindColour(colour, numberX, numberY, numberX_max, numberY_max, tolerance=0):
  79.     """Finds and Matches a pixel colour on a the screen."""
  80.     bbox = {
  81.         "left": numberX,
  82.         "top": numberY,
  83.         "width": numberX_max - numberX,
  84.         "height": numberY_max - numberY,
  85.             }
  86.  
  87.     with mss.mss() as sct:
  88.         img = sct.grab(bbox)
  89.         pixels = zip(img.raw[2::4], img.raw[1::4], img.raw[0::4])
  90.         matchFind = list(pixels)
  91.  
  92.         for i in range(len(matchFind)):
  93.             if matchFind[i] == colour:#exact match
  94.                 areaWidth =  int(i / bbox["width"])
  95.                 totalArea = i - (areaWidth * bbox["width"])#removes total pixles checked from total pixel amount
  96.                 x = numberX + totalArea
  97.                 y =  numberY + areaWidth
  98.                
  99.                 print("found match = {} {}".format((x,y), colour))
  100.                 return x,y
  101.             elif tolerance > 0:
  102.                 hasMatched = True
  103.                 areaWidth =  int(i / bbox["width"])
  104.                 totalArea = i - (areaWidth * bbox["width"])#removes total pixles checked from total pixel amount
  105.                 x = numberX + totalArea
  106.                 y =  numberY + areaWidth
  107.  
  108.                 for index in range(len(colour)):#make sure all r,g,b match all 3
  109.                     if colour[index]-tolerance > (matchFind[i])[index]:#too high
  110.                         hasMatched = False
  111.                     elif colour[index]+tolerance < (matchFind[i])[index]:#too low
  112.                         hasMatched = False
  113.                 if hasMatched:
  114.                     print("tolerance found match = {} {}".format((x,y), colour))
  115.                     return x,y
  116.         return
  117.  
  118. def imageLocation(numberX=None, numberY=None, numberX_max=None, numberY_max=None):
  119.     with mss.mss() as sct:
  120.         if numberX is None:
  121.             bbox = sct.monitors[1]
  122.         else:
  123.             bbox = {
  124.                 "left": numberX,
  125.                 "top": numberY,
  126.                 "width": numberX_max - numberX,
  127.                 "height": numberY_max - numberY,
  128.             }
  129.         return sct.grab(bbox)
  130.  
  131. def imageFind(matchImage, screenImage,numberX=None,numberY=None, tolerance=0.8, imageShape=0):
  132.     """Finds and matches images on a the screen."""
  133.     img_rgb = np.array(screenImage)
  134.     img_gray = cvtColor(img_rgb, COLOR_BGR2GRAY)
  135.     template = imread(matchImage, 0)
  136.    
  137.     res = matchTemplate(img_gray, template, TM_CCOEFF_NORMED)
  138.     min_val, max_val, min_loc, max_loc = minMaxLoc(res)
  139.     x = max_loc[0]+numberX
  140.     y = max_loc[1]+numberY
  141.     xShapeSize = template.shape[1]
  142.     yShapeSize = template.shape[0]
  143.  
  144.     if imageShape == 0 and max_val < tolerance:
  145.         return x,y
  146.     elif max_val < tolerance:
  147.         return x,y,xShapeSize,yShapeSize
  148.     else:
  149.         return
Advertisement
Add Comment
Please, Sign In to add comment