完整代码如下: from PIL import ImageGrab import numpy as np import cv2 from pynput import keyboard import threading import datetime flag = False def video_record(): p = ImageGrab.grab() a, b = p.size fourcc = cv2.VideoWriter_fourcc(*'XVID') filename = '视频_{}.avi'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')) video = cv2.VideoWriter(filename, fourcc, 28, (a, b)) while True: im = ImageGrab.grab() inn = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR) video.write(inn) if flag: print('录制结束!') break video.release() def on_press(key): global flag if key == keyboard.Key.esc: flag = True return False if __name__ == '__main__': print('开始录制!') th = threading.Thread(target=video_record) th.start() with keyboard.Listener(on_press=on_press) as listener: listener.join()在上面代码的基础上加上把它保存为在桌面上的代码
时间: 2024-03-03 16:47:30 浏览: 107
【Linux】Ubuntu18.04使用【python2】安装PIL、opencv、numpy、matplotlib、pandas、seaborn
您可以添加以下代码以将视频保存在桌面上:
```python
import os
desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
filename = os.path.join(desktop_path, '视频_{}.avi'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))
video = cv2.VideoWriter(filename, fourcc, 28, (a, b))
```
这将在桌面上创建一个名为“视频_年月日_时分秒.avi”的文件,其中包含录制的视频。
阅读全文