Use Python scripts to automatically take pictures after the computer wakes up, take screenshots and send email notifications
This article was last updated on: July 24, 2024 am
background
The background is this, my home desktop is perennial dormancy, and configured Wake On Lan (WOL) Easy to wake up and use remotely.
But I found that occasionally the desktop would wake up by other situations, which I didn’t know at this time, and ended up running for days in vain, wasting a lot of power.
So my needs are this:
🤔 The computer wakes upAfter (it may be booted, it may be woken up from hibernation), it automatically does the following:
- Camera to take pictures (to determine if someone is using it)
- Screenshot (to determine if someone is using it)
- Generate an email telling me “Computer is started” with photos and screenshots;
- Send to my mailbox.
Concrete implementation
📷️ Camera to take pictures
✨ Overview:
Pass
opencv-python
Package implementation.
The specific package name is: opencv-python
depend numpy
So the installation command is:
1 |
|
Then the import statement is: import cv2
The source code is as follows:
1 |
|
🎨 Screenshot
✨ Overview:
Pass
pyautogui
Package implementation.
Reference Documentation: 4 ways to get screenshots in Python
pyautogui is relatively simple, but you can’t specify the window to get the program, so the window can’t be occluded, but you can specify the location of the screenshot, 0.04s a screenshot, slightly slower than PyQt, but also fast.
1 |
|
📧 Write a message
✨ Overview:
Pass
MIMEMultipart
type
The different types of content in MIME messages are stored in segments, and the arrangement of each segment and location information are passed Content-Type
domain multipart
type to define. multipart
There are three main subtypes of types:
mixed
:annexalternative
: Plain text and hypertext contentrelated
: Inline resources. For example, when sending email content in HTML format, an image may be used as the background of HTML, and HTML text will be storedalternative
section, and the image as the background is storedrelated
A segment of the type definition
The specific source code is as follows:
1 |
|
📤️ Send an email
✨ Overview:
Pass
smtplib
Package implementation.
The source code is as follows:
1 |
|
⏰ A Python script is triggered after the desktop wakes up
🗔 Windows scripts
The Windows bat script is as follows:
1 |
|
⏰ Task scheduler
enter Computer management -> System tools -> Task scheduler. Add the following task schedule:
- Security options:
- ✔️Check:
不管用户是否登录都要运行
- ✔️Check:
使用最高权限运行
- ✔️Check:
- Trigger:
发生事件时
- Log:
系统
- Source:
Power-Troubleshooter
- Event ID:
1
- Action: Start the program:
D:\scripts\auto_email.bat