python屏幕录制模拟点击事件_python 实现屏幕录制示例

PIL 即pollow 的安装命令如下: pip install pillow

其中cv2的安装是下面这条命令 pip install opencv-python

代码实现: # coding: utf-8

from PIL import ImageGrab

import numpy as np

import cv2

fps = 20

start = 3 # 延时录制

end = 15 # 自动结束时间

curScreen = ImageGrab.grab() # 获取屏幕对象

height, width = curScreen.size

video = cv2.VideoWriter('video02.avi', cv2.VideoWriter_fourcc(*'XVID'), fps, (height, width))

imageNum = 0

while True:

imageNum += 1

captureImage = ImageGrab.grab() # 抓取屏幕

frame = cv2.cvtColor(np.array(captureImage), cv2.COLOR_RGB2BGR)

# 显示无图像的窗口

cv2.imshow('capturing', np.zeros((1, 255), np.uint8))

# 控制窗口显示位置,方便通过按键方式退出

cv2.moveWindow('capturing', height - 100, width - 100)

if imageNum > fps * start:

video.write(frame)

# 退出条件

if cv2.waitKey(50) == ord('q') or imageNum > fps * end:

break

video.release()

cv2.destroyAllWindows()

以上这篇python 实现屏幕录制示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持聚米学院。

可以使用 Python 的 PyAutoGUI 库来模拟用户鼠标操作录制。该库可以在 Windows、Mac 和 Linux 等操作系统上使用。 以下是一个示例代码,可以模拟用户在屏幕上单击、拖动和释放鼠标,然后将这些操作保存到文件中: ```python import pyautogui import time # 记录鼠标操作的列表 actions = [] # 开始录制 print('Recording started...') print('Press Ctrl-C to stop.') time.sleep(2) # 等待 2 秒钟,以便打开需要操作的窗口 try: while True: x, y = pyautogui.position() # 获取鼠标的当前位置 action = {'x': x, 'y': y} if pyautogui.mouseDown(): action['event'] = 'mousedown' elif pyautogui.mouseUp(): action['event'] = 'mouseup' elif pyautogui.drag(): action['event'] = 'drag' actions.append(action) time.sleep(0.1) # 等待 0.1 秒钟,以便捕获足够的动作 except KeyboardInterrupt: # 用户按下了 Ctrl-C,停止录制并保存操作列表 print('Recording stopped.') filename = input('Enter filename to save actions: ') with open(filename, 'w') as f: f.write(str(actions)) print(f'Actions saved to {filename}.') ``` 该代码会在控制台输出一些提示信息,然后等待 2 秒钟以便打开需要操作的窗口。然后,它会进入一个无限循环,捕获鼠标的位置和动作,并将它们添加到 `actions` 列表中。当用户按下 Ctrl-C 时,程序会停止录制并要求用户输入一个文件名,然后将所有操作保存到该文件中。 可以使用以下代码来播放保存的操作: ```python import ast import pyautogui import time # 读取保存的操作列表 filename = input('Enter filename to play: ') with open(filename, 'r') as f: actions = ast.literal_eval(f.read()) # 开始播放操作 print('Playing actions...') print('Press Ctrl-C to stop.') time.sleep(2) # 等待 2 秒钟,以便打开需要操作的窗口 try: for action in actions: x, y = action['x'], action['y'] if 'event' not in action: pyautogui.moveTo(x, y) elif action['event'] == 'mousedown': pyautogui.click(x, y, button='left') elif action['event'] == 'mouseup': pyautogui.mouseUp() elif action['event'] == 'drag': pyautogui.dragTo(x, y) time.sleep(0.1) # 等待 0.1 秒钟,以便模拟真实操作的速度 except KeyboardInterrupt: # 用户按下了 Ctrl-C,停止播放操作 print('Playback stopped.') ``` 该代码会要求用户输入保存操作的文件名,然后读取该文件并播放其中保存的操作。与录制代码类似,该代码也会等待 2 秒钟以便打开需要操作的窗口。在播放过程中,它会模拟用户鼠标操作,并将它们发送到屏幕上。当用户按下 Ctrl-C 时,程序会停止播放。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值