Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mss
- import numpy as np
- from time import sleep, strftime, time
- from random import uniform, randint
- from keyboard import wait, send, add_hotkey, on_press, is_pressed, press as kpress, release as krelease
- from mouse import get_position, move, press, release, wheel
- from cv2 import cvtColor, COLOR_BGR2GRAY, matchTemplate, TM_CCOEFF_NORMED, minMaxLoc, imread
- def humanMove(numberX, numberY, mincount, maxcount, minmove, maxmove):
- """ Humanize moves the mouse cursor to a point on the screen. """
- x, y = get_position()
- randomNumber = randint(mincount, maxcount)
- x2, y2 = (numberX - x) / randomNumber, (numberY - y) / randomNumber
- for number in range(0, number):
- moveSpeed = uniform(0.04, 0.09)
- offset = randint(minmove, maxmove)
- move(x2 + offset, y2 + offset, False,moveSpeed)
- move(numberX, numberY,duration=moveSpeed)
- def humanMoverel(numberX, numberY, mincount, maxcount, minmove, maxmove):
- """ Humanize moves the mouse cursor to a point on the screen, relative to its current
- position. """
- randomNumber = randint(mincount, maxcount)
- x, y = numberX / randomNumber, numberY / randomNumber
- for number in range(0, number):
- moveSpeed = uniform(0.04, 0.09)
- offset = randint(minmove, maxmove)
- move(x + offset, y, False,duration=moveSpeed)
- def humanClick(action):
- """ Sends a humanize click with the given button. """
- time_to_wait = uniform(0.047, 0.378)
- press(action)
- sleep(time_to_wait)
- release(action)
- def humanKeyp(key):
- """ Humanize presses a given key. """
- time_to_wait = uniform(0.047, 0.378)
- kpress(key)
- sleep(time_to_wait)
- krelease(key)
- def grabPixel(numberX, numberY):
- """ Grabs a pixel colour on a point of the screen."""
- with mss.mss() as sct:
- try:
- sct_img = sct.grab(sct.monitors[1])
- return sct_img.pixel(numberX, numberY)
- except:
- mon = sct.monitors[2]
- sct_img = sct.grab(mon)
- return sct_img.pixel(numberX-mon["left"], numberY)
- def pixelMatchesColor(colour, match, tolerance=0):
- """Matches a pixel colour on a point of the screen."""
- # should always be 3 or 4 (rbg, possibly with an alpha channel)
- if colour == match:#exact match
- return True
- has_matched = True
- if tolerance > 0:
- for index in range(len(colour)):# make sure all 3 match
- if colour[index]-tolerance > match[index]:# too high
- has_matched = False
- elif colour[index]+tolerance < match[index]:# too low
- has_matched = False
- if has_matched == True:
- return True
- else:
- return False
- def pixelFindColour(colour, numberX, numberY, numberX_max, numberY_max, tolerance=0):
- """Finds and Matches a pixel colour on a the screen."""
- bbox = {
- "left": numberX,
- "top": numberY,
- "width": numberX_max - numberX,
- "height": numberY_max - numberY,
- }
- with mss.mss() as sct:
- img = sct.grab(bbox)
- pixels = zip(img.raw[2::4], img.raw[1::4], img.raw[0::4])
- matchFind = list(pixels)
- for i in range(len(matchFind)):
- if matchFind[i] == colour:#exact match
- areaWidth = int(i / bbox["width"])
- totalArea = i - (areaWidth * bbox["width"])#removes total pixles checked from total pixel amount
- x = numberX + totalArea
- y = numberY + areaWidth
- print("found match = {} {}".format((x,y), colour))
- return x,y
- elif tolerance > 0:
- hasMatched = True
- areaWidth = int(i / bbox["width"])
- totalArea = i - (areaWidth * bbox["width"])#removes total pixles checked from total pixel amount
- x = numberX + totalArea
- y = numberY + areaWidth
- for index in range(len(colour)):#make sure all r,g,b match all 3
- if colour[index]-tolerance > (matchFind[i])[index]:#too high
- hasMatched = False
- elif colour[index]+tolerance < (matchFind[i])[index]:#too low
- hasMatched = False
- if hasMatched:
- print("tolerance found match = {} {}".format((x,y), colour))
- return x,y
- return
- def imageLocation(numberX=None, numberY=None, numberX_max=None, numberY_max=None):
- with mss.mss() as sct:
- if numberX is None:
- bbox = sct.monitors[1]
- else:
- bbox = {
- "left": numberX,
- "top": numberY,
- "width": numberX_max - numberX,
- "height": numberY_max - numberY,
- }
- return sct.grab(bbox)
- def imageFind(matchImage, screenImage,numberX=None,numberY=None, tolerance=0.8, imageShape=0):
- """Finds and matches images on a the screen."""
- img_rgb = np.array(screenImage)
- img_gray = cvtColor(img_rgb, COLOR_BGR2GRAY)
- template = imread(matchImage, 0)
- res = matchTemplate(img_gray, template, TM_CCOEFF_NORMED)
- min_val, max_val, min_loc, max_loc = minMaxLoc(res)
- x = max_loc[0]+numberX
- y = max_loc[1]+numberY
- xShapeSize = template.shape[1]
- yShapeSize = template.shape[0]
- if imageShape == 0 and max_val < tolerance:
- return x,y
- elif max_val < tolerance:
- return x,y,xShapeSize,yShapeSize
- else:
- return
Advertisement
Add Comment
Please, Sign In to add comment