dustyroo

Untitled

Aug 31st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 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.  
  10. def humanMove(numberX, numberY, randomMinimum=1, randomMaximum=1, minMove=1, maxMove=1):
  11.     '''Humanize moves the mouse cursor to a point on the screen.'''
  12.     x, y = get_position()
  13.     randomNumber = randint(randomMinimum, randomMaximum)
  14.     x2, y2 = (numberX - x) / randomNumber, (numberY - y) / randomNumber
  15.  
  16.     for number in range(randomNumber):
  17.         moveSpeed = uniform(0.04, 0.09)
  18.         offset = randint(minMove, maxMove)
  19.         move(x2 + offset, y2 + offset, False,moveSpeed)
  20.  
  21.     move(numberX, numberY,duration=moveSpeed)
  22.  
  23. def humanMoverel(numberX, numberY,randomMinimum=0, randomMaximum=0, minMove=0, maxMove=0):
  24.     """ Humanize moves the mouse cursor to a point on the screen, relative to its current
  25.    position. """
  26.     randomNumber = randint(randomMinimum, randomMaximum)
  27.     x, y = numberX / randomNumber, numberY / randomNumber
  28.  
  29.     for number in range(randomNumber):  
  30.         moveSpeed = uniform(0.04, 0.09)
  31.         offset = randint(minMove, maxMove)
  32.  
  33.         move(x + offset, y, False,duration=moveSpeed)
  34.  
  35. def humanClick(action):
  36.     """ Sends a humanize click with the given button. """
  37.     time_to_wait = uniform(0.047, 0.378)
  38.     press(action)
  39.     sleep(time_to_wait)
  40.     release(action)
  41. def imageLocation(numberX=None, numberY=None, numberXMax=None, numberYMax=None):
  42.     with mss.mss() as sct:
  43.         if numberX is None:
  44.             box = sct.monitors[1]
  45.         else:
  46.             box = {
  47.                 "left": numberX,
  48.                 "top": numberY,
  49.                 "width": numberXMax - numberX,
  50.                 "height": numberYMax - numberY,
  51.             }
  52.         return sct.grab(box)
  53.  
  54. def imageFind(matchImage, screenImage,numberX=None,numberY=None, tolerance = 0.7, imageShape=0, boolValue=0):
  55.     """Finds and matches images on a the screen."""
  56.     try:
  57.         img_rgb = np.array(screenImage)
  58.         img_gray = cvtColor(img_rgb, COLOR_BGR2GRAY)
  59.         template = imread(matchImage, 0)
  60.        
  61.         res = matchTemplate(img_gray, template, TM_CCOEFF_NORMED)
  62.         min_val, max_val, min_loc, max_loc = minMaxLoc(res)
  63.         x = max_loc[0]+numberX
  64.         y = max_loc[1]+numberY
  65.         xShapeSize = template.shape[1]
  66.         yShapeSize = template.shape[0]
  67.         if boolValue is 1:
  68.             if max_val < tolerance:
  69.                 return False
  70.             else:
  71.                 return True
  72.         if imageShape is 1:
  73.             if max_val < tolerance:
  74.                 return 0,0,0,0
  75.             else:
  76.                 return x,y,xShapeSize, yShapeSize
  77.         if max_val < tolerance:
  78.             return 0,0
  79.         else:
  80.             return x,y
  81.     except Exception as e:
  82.         appendFile = open('logfile_startdata_errors.txt','a')
  83.         appendFile.write('ERROR(imageFind()) @{}  img:{} Info {}\n'.format(strftime("%c"),matchImage,e))
  84.         appendFile.close()
  85. def pixelFindColour(colour, numberX, numberY, numberXMax, numberYMax, tolerance=0):
  86.     """Finds and Matches a pixel colour on screen."""
  87.     box = {
  88.         "left": numberX,
  89.         "top": numberY,
  90.         "width": numberXMax - numberX,
  91.         "height": numberYMax - numberY,
  92.             }
  93.  
  94.     with mss.mss() as sct:
  95.         img = sct.grab(box)
  96.         pixels = zip(img.raw[2::4], img.raw[1::4], img.raw[0::4])
  97.  
  98.         for checked,matchFind in enumerate(pixels):
  99.             #print(matchFind)
  100.             if matchFind == colour:#exact match
  101.                 areaWidth =  int(checked / box["width"])
  102.                 totalArea = checked - (areaWidth * box["width"])#removes checked pixles from total pixels
  103.                 x = numberX + totalArea
  104.                 y =  numberY + areaWidth
  105.                 #print("found match = {} {}".format((x,y), colour))
  106.                 return True#x,y
  107.             elif tolerance > 0:
  108.                 hasMatched = True
  109.                 for index in range(len(colour)):#make sure all r,g,b match all 3
  110.                     if colour[index]-tolerance > matchFind[index]:#too high
  111.                         hasMatched = False
  112.                     elif colour[index]+tolerance < matchFind[index]:#too low
  113.                         hasMatched = False
  114.                 if hasMatched:
  115.                     areaWidth =  int(checked / box["width"])
  116.                     totalArea = checked - (areaWidth * box["width"])#removes checked pixles from total pixels
  117.                     x = numberX + totalArea
  118.                     y =  numberY + areaWidth
  119.                     #print("tolerance found match = {} {}".format((x,y), colour))
  120.                     return True#x,y
  121.                    
  122.         #print('Search done no match, retry!')
  123.         return False
Add Comment
Please, Sign In to add comment