Home > Net >  PyAutoGUI random click within area in a radial-like pattern
PyAutoGUI random click within area in a radial-like pattern

Time:11-22

pretty new to python, but I'm trying to have the mouse click on a point within an image using PyAutoGUI. However the project requires I simulate a "human pattern". So what I'm going for is an "accurate-like" accuracy, where most of the points are in the middle and it gets more sparse the further away the click is, simulating missclicks or room for error. So that not every click is exactly on the point in the centre. Check the simulated clickmap below:

clickmap

(where red is the most clicked area and green is the least - each click is represented by a pixel)

import pyautogui

pyautogui.click(pos.x,pos.y)

Given that I have the x and y position, what's the best way to achieve this kind of somewhat random pattern the most efficient way?

CodePudding user response:

Generate a random number and add into your x & y positions

import random
from PIL import Image

object = Image.open('Screenshot.png')
theWidth = object.width
theHeight = object.height
X = pos.x   random.randint(pos.x - theWidth/2,pos.x   theWidth/2)
Y = pos.y   random.randint(pos.y - theHeight/2,pos.y   theHeight/2)

CodePudding user response:

I would use np.random.normal() to generate clicks within a normal distribution (vertically and horizontally). I also randomise the interval between clicks to generate a more "human" click rate.

import pyautogui as p
import numpy as np
import random

def clicker(x, y, sigma, n_clicks):
    s = np.random.normal(x, sigma, n_clicks)
    t = np.random.normal(y, sigma, n_clicks)
    
    for i in range(n_clicks):
        p.moveTo(s[i],t[i],0.2)
        p.click(s[i],t[i])
        p.sleep(random.random())

#Set the number of clicks
number_of_clicks = 100

# Set the centre of the image
centreX = 500
centreY = 500

# Set the standard deviation (1SD in pixels)
sd = 100

# Sleep for 5 seconds before starting
p.sleep(5) 
clicker(centreX, centreY, sd, number_of_clicks)

CLICK OUTPUT using 100 clicks:

Click output

  • Related