python 实现屏幕录制示例

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

相关文章

为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)

为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)

为何人工智能(AI)首选Python?读完这篇文章你就知道了。我们看谷歌的TensorFlow基本上所有的代码都是C++和Python,其他语言一般只有几千行 。如果讲运行速度的部分,...

python高斯分布概率密度函数的使用详解

python高斯分布概率密度函数的使用详解

如下所示: import matplotlib.pyplot as plt import numpy as np from scipy import stats from matpl...

python判断字符串是否是json格式方法分享

在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合‘Pythonic'写法 代码示例: Python import json...

Python常见数据结构详解

本文详细罗列归纳了Python常见数据结构,并附以实例加以说明,相信对读者有一定的参考借鉴价值。 总体而言Python中常见的数据结构可以统称为容器(container)。而序列(如列表...

Python中str is not callable问题详解及解决办法

Python中str is not callable问题详解及解决办法 问题提出:    在Python的代码,在运行过程中,碰到了一个错误信息:  &nb...