gpt4 book ai didi

python - Pyautogui:带贝塞尔曲线的鼠标移动

转载 作者:太空狗 更新时间:2023-10-29 17:18:08 32 4
gpt4 key购买 nike

我试图在 Pyautogui 中以贝塞尔曲线运动移动鼠标,以模拟更多的人体运动,如下所示: enter image description here

pyautogui 中有一些补间/缓动功能,但没有一个代表贝塞尔曲线类型的移动。我创建了一个小脚本来计算在最终到达目的地之前它将到达的随机位置。

默认“机器人”线性路径: enter image description here

不幸的是,鼠标暂时停在了每个目的地。

import pyautogui
import time
import random
print "Randomized Mouse Started."
destx = 444;
desty = 631;
x, y = pyautogui.position() # Current Position
moves = random.randint(2,4)
pixelsx = destx-x
pixelsy = desty-y
if moves >= 4:
moves = random.randint(2,4)
avgpixelsx = pixelsx/moves
avgpixelsy = pixelsy/moves
print "Pixels to be moved X: ", pixelsx," Y: ",pixelsy, "Number of mouse movements: ", moves, "Avg Move X: ", avgpixelsx, " Y: ", avgpixelsy

while moves > 0:
offsetx = (avgpixelsx+random.randint(-8, random.randint(5,10)));
offsety = (avgpixelsy+random.randint(-8, random.randint(5,10)));
print x + offsetx, y + offsety, moves
pyautogui.moveTo(x + offsetx, y + offsety, duration=0.2)
moves = moves-1
avgpixelsx = pixelsx / moves
avgpixelsy = pixelsy / moves

信息:

  • Windows 10
  • python 2.7
  • 愿意使用其他库,必要时使用 Python 版本

我看过这篇文章:python random mouse movements

但不知道如何定义“开始和停止”位置。答案与我正在寻找的非常接近。

关于如何实现这一点有什么想法吗?

最佳答案

使用 scipy、numpy 和任何可以简单地移动鼠标光标的工具:

import pyautogui
import random
import numpy as np
import time
from scipy import interpolate
import math

def point_dist(x1,y1,x2,y2):
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

cp = random.randint(3, 5) # Number of control points. Must be at least 2.
x1, y1 = pyautogui.position() # Starting position

# Distribute control points between start and destination evenly.
x = np.linspace(x1, x2, num=cp, dtype='int')
y = np.linspace(y1, y2, num=cp, dtype='int')

# Randomise inner points a bit (+-RND at most).
RND = 10
xr = [random.randint(-RND, RND) for k in range(cp)]
yr = [random.randint(-RND, RND) for k in range(cp)]
xr[0] = yr[0] = xr[-1] = yr[-1] = 0
x += xr
y += yr

# Approximate using Bezier spline.
degree = 3 if cp > 3 else cp - 1 # Degree of b-spline. 3 is recommended.
# Must be less than number of control points.
tck, u = interpolate.splprep([x, y], k=degree)
# Move upto a certain number of points
u = np.linspace(0, 1, num=2+int(point_dist(x1,y1,x2,y2)/50.0))
points = interpolate.splev(u, tck)

# Move mouse.
duration = 0.1
timeout = duration / len(points[0])
point_list=zip(*(i.astype(int) for i in points))
for point in point_list:
pyautogui.moveTo(*point)
time.sleep(timeout)

并且您可以通过设置删除 pyautogui 中的任何内置延迟:

# Any duration less than this is rounded to 0.0 to instantly move the mouse.
pyautogui.MINIMUM_DURATION = 0 # Default: 0.1
# Minimal number of seconds to sleep between mouse moves.
pyautogui.MINIMUM_SLEEP = 0 # Default: 0.05
# The number of seconds to pause after EVERY public function call.
pyautogui.PAUSE = 0 # Default: 0.1

P.S.:上面的示例不需要任何这些设置,因为它不使用公共(public) moveTo 方法。

关于python - Pyautogui:带贝塞尔曲线的鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44467329/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com