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, randomMinimum=1, randomMaximum=1, minMove=1, maxMove=1):
- '''Humanize moves the mouse cursor to a point on the screen.'''
- x, y = get_position()
- randomNumber = randint(randomMinimum, randomMaximum)
- x2, y2 = (numberX - x) / randomNumber, (numberY - y) / randomNumber
- for number in range(randomNumber):
- 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,randomMinimum=0, randomMaximum=0, minMove=0, maxMove=0):
- """ Humanize moves the mouse cursor to a point on the screen, relative to its current
- position. """
- randomNumber = randint(randomMinimum, randomMaximum)
- x, y = numberX / randomNumber, numberY / randomNumber
- for number in range(randomNumber):
- 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 imageLocation(numberX=None, numberY=None, numberXMax=None, numberYMax=None):
- with mss.mss() as sct:
- if numberX is None:
- box = sct.monitors[1]
- else:
- box = {
- "left": numberX,
- "top": numberY,
- "width": numberXMax - numberX,
- "height": numberYMax - numberY,
- }
- return sct.grab(box)
- def imageFind(matchImage, screenImage,numberX=None,numberY=None, tolerance = 0.7, imageShape=0, boolValue=0):
- """Finds and matches images on a the screen."""
- try:
- 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 boolValue is 1:
- if max_val < tolerance:
- return False
- else:
- return True
- if imageShape is 1:
- if max_val < tolerance:
- return 0,0,0,0
- else:
- return x,y,xShapeSize, yShapeSize
- if max_val < tolerance:
- return 0,0
- else:
- return x,y
- except Exception as e:
- appendFile = open('logfile_startdata_errors.txt','a')
- appendFile.write('ERROR(imageFind()) @{} img:{} Info {}\n'.format(strftime("%c"),matchImage,e))
- appendFile.close()
- def pixelFindColour(colour, numberX, numberY, numberXMax, numberYMax, tolerance=0):
- """Finds and Matches a pixel colour on screen."""
- box = {
- "left": numberX,
- "top": numberY,
- "width": numberXMax - numberX,
- "height": numberYMax - numberY,
- }
- with mss.mss() as sct:
- img = sct.grab(box)
- pixels = zip(img.raw[2::4], img.raw[1::4], img.raw[0::4])
- for checked,matchFind in enumerate(pixels):
- #print(matchFind)
- if matchFind == colour:#exact match
- areaWidth = int(checked / box["width"])
- totalArea = checked - (areaWidth * box["width"])#removes checked pixles from total pixels
- x = numberX + totalArea
- y = numberY + areaWidth
- #print("found match = {} {}".format((x,y), colour))
- return True#x,y
- elif tolerance > 0:
- hasMatched = True
- for index in range(len(colour)):#make sure all r,g,b match all 3
- if colour[index]-tolerance > matchFind[index]:#too high
- hasMatched = False
- elif colour[index]+tolerance < matchFind[index]:#too low
- hasMatched = False
- if hasMatched:
- areaWidth = int(checked / box["width"])
- totalArea = checked - (areaWidth * box["width"])#removes checked pixles from total pixels
- x = numberX + totalArea
- y = numberY + areaWidth
- #print("tolerance found match = {} {}".format((x,y), colour))
- return True#x,y
- #print('Search done no match, retry!')
- return False
Add Comment
Please, Sign In to add comment