Python屏幕录制 只用20行Python代码实现屏幕录制功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python屏幕录制 只用20行Python代码实现屏幕录制功能

L e x   2021-06-02 我要评论
想了解只用20行Python代码实现屏幕录制功能的相关内容吗,L e x在本文为您仔细讲解Python屏幕录制的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python屏幕录制,python监控,下面大家一起来学习吧。

一、模块安装

首先,我们需要用到两个python的两个模块,win32gui和PyQt5

1.pip install win32gui

2.pip install PyQt5

1、pip install win32gui
PS C:\Users\lex\Desktop> pip install win32gui
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Requirement already satisfied: win32gui in f:\develop\python36\lib\site-packages (221.6)
Requirement already satisfied: win32core in f:\develop\python36\lib\site-packages (from win32gui) (221.36)
 
2、pip install PyQt5
PS C:\Users\lex\Desktop> pip install PyQt5
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Requirement already satisfied: PyQt5 in f:\develop\python36\lib\site-packages (5.15.4)
Requirement already satisfied: PyQt5-sip<13,>=12.8 in f:\develop\python36\lib\site-packages (from PyQt5) (12.8.1)
Requirement already satisfied: PyQt5-Qt5>=5.15 in f:\develop\python36\lib\site-packages (from PyQt5) (5.15.2)
PS C:\Users\lex\Desktop>

二、设计思路

1.通过win32gui模块,调用windows系统的截屏功能,对屏幕进行录制。

2.通过timer定时器,实现每隔2秒钟,截屏一次,从而记录屏幕使用者的操作记录。

3.对截取的屏幕按照 截取时间进行命名,并存储到一个比较隐秘的文件夹路径中。

这样,我们通过,查看文件夹中的照片,就可以清晰的掌握 不明登陆者,在电脑上进行了什么样的操作。

三、代码实现

只需要简简单单10多行代码,就可以实现对windows系统电脑屏幕的录制。

代码如下:

#定义函数,每个2秒 抓取一次屏幕截屏
def timer(n):
    while True:  
        dt= time.strftime('%Y-%m-%d %H%M%S',time.localtime())
        screen = QApplication.primaryScreen()
        img = screen.grabWindow(record).toImage()
        img.save("D:\\images\\"+dt+".jpg")
        time.sleep(n)
if __name__ == "__main__":
    timer(2)

四、运行方法

1.python环境检查

命令行运行 python,查看python版本为3.6.5

PS C:\Users\lex\Desktop> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

2.程序运行

命令行运行 python py-screen.py 即可,效果如下图:

五、完整代码

from PyQt5.QtWidgets import QApplication
import win32gui
import sys
import time
record = win32gui.FindWindow(None, 'C:\Windows\system32\cmd.exe')
app = QApplication(sys.argv)
def timer(n):
    while True:  
        dt= time.strftime('%Y-%m-%d %H%M%S',time.localtime())
        screen = QApplication.primaryScreen()
        img = screen.grabWindow(record).toImage()
        img.save("D:\\images\\"+dt+".jpg")
        time.sleep(n)
if __name__ == "__main__":
    timer(2)

六、故事结尾

最后,看了一下电脑截图

原来只是在直播而已

还好还好,python小哥哥终于可以放心的睡了

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们