


How to use a Python script to automatically take photos, take screenshots and send email notifications after the computer wakes up
背景
背景是这样的, 我的家里台式机常年 休眠, 并配置了 Wake On Lan (WOL) 方便远程唤醒并使用.
但是我发现, 偶尔台式机会被其他情况唤醒, 这时候我并不知道, 结果白白运行了好几天, 浪费了很多电.
所以我的需求是这样的:
电脑唤醒后(可能是开机, 有可能是从休眠状态唤醒), 自动做如下几件事:
摄像头拍照(判断是不是有人在使用)
屏幕截图(判断是不是有人在使用)
生成一封邮件, 告诉我「电脑已启动」并附上拍照和截图;
发送到我的邮箱.
具体实现
️ 摄像头拍照
概述:
通过
opencv-python
包实现.
具体的包名为: opencv-python
依赖 numpy
所以安装命令为:
python -m pip install numpy python -m pip install opencv-python
然后导入语句为: import cv2
源码如下:
# 打开摄像头并拍照 cap = cv2.VideoCapture(0) # 0 表示打开 PC 的内置摄像头(若参数是视频文件路径则打开视频) # 按帧读取图片或视频 # ret,frame 是 cap.read() 方法的两个返回值。 # 其中 ret 是布尔值,如果读取帧是正确的则返回 True,如果文件读取到结尾,它的返回值就为 False。 # frame 就是每一帧的图像,是个三维矩阵。 ret, frame = cap.read() # 按帧读取图片 cv2.imwrite('p1.jpg', frame) # 保存图像 cap.release() # 释放(关闭)摄像头
屏幕截图
概述:
通过
pyautogui
包实现.
pyautogui 是比较简单的,但是不能指定获取程序的窗口,因此窗口也不能遮挡,不过可以指定截屏的位置,0.04s 一张截图,比 PyQt 稍慢一点,但也很快了。
import pyautogui import cv2 # 截图 screen_shot = pyautogui.screenshot() screen_shot.save('screenshot.png')
写邮件
概述:
通过
MIMEMultipart
类型
MIME 邮件中各种不同类型的内容是分段存储的,各个段的排列方式、位置信息都通过 Content-Type
域的 multipart
类型来定义。 multipart
类型主要有三种子类型:
mixed
: 附件alternative
: 纯文本和超文本内容related
:内嵌资源. 比如:在发送 html 格式的邮件内容时,可能使用图像作为 html 的背景,html 文本会被存储在alternative
段中,而作为背景的图像则会存储在related
类型定义的段中
具体源码如下:
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage sender = 'admin@example.com' # 发件人 receivers = 'admin@example.com' # 收件人 pw = 'p@ssw0rd' # 三方客户端登录邮箱授权码 subject = '电脑已启动拍照并发送' # 邮件主题 text = '您好,您的电脑已开机,并拍摄了如下照片:' # 邮件正文 msg = MIMEMultipart('mixed') # 定义含有附件类型的邮件 msg['Subject'] = subject # 邮件主题 msg['From'] = sender # 发件人 msg['To'] = receivers # 收件人 # MIMEText三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 # 构造一个文本邮件对象, plain 原格式输出; html html格式输出 text = MIMEText(text, 'plain', 'utf-8') msg.attach(text) # 将文本内容添加到邮件中 for i in ('p1.jpg', 'screenshot.png'): sendImg = open(i, 'rb').read() # 读取刚才的图片 img = MIMEImage(sendImg) # 构造一个图片附件对象 # 指定下载的文件类型为:附件, 并加上文件名 img['Content-Disposition'] = 'attachment; filename={}'.format(i) msg.attach(img) # 将附件添加到邮件中 msg_tsr = msg.as_string() # 将msg对象变为str
️ 发邮件
概述:
通过
smtplib
包实现.
源码如下:
import smtplib # 发送邮件 try: smtp = smtplib.SMTP() # 定义一个SMTP(传输协议)对象 smtp.connect('smtp.example.com', 25) # 连接到邮件发送服务器,默认25端口 smtp.login(sender, pw) # 使用发件人邮件及授权码登陆 smtp.sendmail(sender, receivers, msg_tsr) # 发送邮件 smtp.quit() # 关闭邮箱,退出登陆 except Exception as e: print('\033[31;1m出错了:%s\033[0m' % (e)) else: print('邮件发送成功!')
台式机唤醒后触发 python 脚本
Windows 脚本
Windows bat 脚本如下:
@echo off timeout /T 15 /NOBREAK # sleep 15s cd /d D:\scripts\auto_send_email python auto_email.py # 执行py文件
任务计划程序
进入 计算机管理 -> 系统工具 -> 任务计划程序. 添加如下任务计划:
安全选项:
勾选:
不管用户是否登录都要运行
勾选:
使用最高权限运行
触发器:
发生事件时
日志:
系统
源:
Power-Troubleshooter
事件 ID:
1
操作: 启动程序:
D:\scripts\auto_email.bat
The above is the detailed content of How to use a Python script to automatically take photos, take screenshots and send email notifications after the computer wakes up. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
