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

相关文章

cProfile Python性能分析工具使用详解

cProfile Python性能分析工具使用详解

前言 Python自带了几个性能分析的模块:profile、cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的。本文介绍cProfile。 例子...

python正常时间和unix时间戳相互转换的方法

本文实例讲述了python正常时间和unix时间戳相互转换的方法。分享给大家供大家参考。具体分析如下: 这段代码可以用来转换常规时间格式为unix时间戳,也可以将unix时间戳转换回来,...

解决python 无法加载downsample模型的问题

downsample 在最新版本里面修改了位置 from theano.tensor.single import downsample (旧版本) 上面以上的的import会有error...

Python实现基于SVM的分类器的方法

本文代码来之《数据分析与挖掘实战》,在此基础上补充完善了一下~ 代码是基于SVM的分类器Python实现,原文章节题目和code关系不大,或者说给出已处理好数据的方法缺失、源是图像数据更...

PyCharm更改字体和界面样式的方法步骤

PyCharm更改字体和界面样式的方法步骤

更改主题 File → Settings → Appearance & Behavior → Appearance → Theme 结果: 更改字体大小 File → Sett...