Pyautogui:具有贝塞尔曲线的鼠标移动

时间:2017-06-09 22:08:24

标签: python random pyautogui

我正试图在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

但无法弄清楚如何定义“开始和停止”位置。答案非常接近我正在寻找的东西。

关于如何实现这一目标的任何想法?

3 个答案:

答案 0 :(得分:10)

使用scipy和任何可以简单移动鼠标光标的东西:

import pyautogui
import random
import scipy
import time
from scipy import interpolate


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

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

# Randomise inner points a bit (+-RND at most).
RND = 10
xr = scipy.random.randint(-RND, RND, size=cp)
yr = scipy.random.randint(-RND, RND, size=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 = scipy.interpolate.splprep([x, y], k=degree)
u = scipy.linspace(0, 1, num=max(pyautogui.size()))
points = scipy.interpolate.splev(u, tck)

# Move mouse.
duration = 0.2
timeout = duration / len(points[0])
for point in zip(*(i.astype(int) for i in points)):
    pyautogui.platformModule._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。:上面的示例不需要任何这些设置,因为它不使用公共moveTo方法。

答案 1 :(得分:3)

对于一个简单的解决方案,您可以尝试将 numpybezier 库结合使用:

import pyautogui
import bezier
import numpy as np


# Disable pyautogui pauses (from DJV's answer)
pyautogui.MINIMUM_DURATION = 0
pyautogui.MINIMUM_SLEEP = 0
pyautogui.PAUSE = 0

# We'll wait 5 seconds to prepare the starting position
start_delay = 5 
print("Drawing curve from mouse in {} seconds.".format(start_delay))
pyautogui.sleep(start_delay)

# For this example we'll use four control points, including start and end coordinates
start = pyautogui.position()
end = start[0]+600, start[1]+200
# Two intermediate control points that may be adjusted to modify the curve.
control1 = start[0]+125, start[1]+100
control2 = start[0]+375, start[1]+50

# Format points to use with bezier
control_points = np.array([start, control1, control2, end])
points = np.array([control_points[:,0], control_points[:,1]]) # Split x and y coordinates

# You can set the degree of the curve here, should be less than # of control points
degree = 3
# Create the bezier curve
curve = bezier.Curve(points, degree)
# You can also create it with using Curve.from_nodes(), which sets degree to len(control_points)-1
# curve = bezier.Curve.from_nodes(points)

curve_steps = 50  # How many points the curve should be split into. Each is a separate pyautogui.moveTo() execution
delay = 1/curve_steps  # Time between movements. 1/curve_steps = 1 second for entire curve

# Move the mouse
for i in range(1, curve_steps+1):
    # The evaluate method takes a float from [0.0, 1.0] and returns the coordinates at that point in the curve
    # Another way of thinking about it is that i/steps gets the coordinates at (100*i/steps) percent into the curve
    x, y = curve.evaluate(i/curve_steps)
    pyautogui.moveTo(x, y)  # Move to point in curve
    pyautogui.sleep(delay)  # Wait delay

我想出了这个,试图写一些东西来用鼠标画 SVG Paths。运行上面的代码将使您的鼠标沿着与下面相同的路径移动。红点位于定义曲线的每个控制点处。

请注意,如果您想像我在 GIMP 中所做的那样单击并拖动,则必须在脚本末尾的循环之前和之后添加 pyautogui.mouseDown()pyautogui.mouseUp()Path of Bezier curve as dr

您可以在此处查看 bezier 文档:https://bezier.readthedocs.io/en/stable/index.html

答案 2 :(得分:2)

你只需要知道move_mouse((300,300))会让你鼠标到达(300,300),然后永远不会改变。看看工具,它只是调用WIN32 api mouse_event。阅读一些关于它的信息,你会发现没有"开始和停止" position.i不知道如何绘制贝塞尔曲线。

    while True:
        pos = (random.randrange(*x_bound),random.randrange(*y_bound))
        move_mouse(pos)
        time.sleep(1.0/steps_per_second)

看,这是动画的秘密。你需要做的就是写一个pos = draw_bezier_curve(t)