python 实现屏幕录制示例

yipeiwu_com6年前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设计】。

相关文章

详解python websocket获取实时数据的几种常见链接方式

第一种, 使用create_connection链接,需要pip install websocket-client (此方法不建议使用,链接不稳定,容易断,并且连接很耗时) imp...

python enumerate内置函数用法总结

python enumerate内置函数用法总结

这篇文章主要介绍了python enumerate内置函数用法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 enumera...

Python实现发送email的几种常用方法

学过Python的人都知道,实用Python实现发送email的功能还是比较简单的,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是...

python读取tif图片时保留其16bit的编码格式实例

tif图片的编码格式一般是16bit的,在使用python-opencv读取tif文件时,为了保留其编码格式,我们需要用以下的方式: import numpy as np impor...

selenium + python 获取table数据的示例讲解

方法一: <code class="language-python">""" 根据table的id属性和table中的某一个元素定位其在table中的位置 table...