Operations 7 min read

Python Script for Capturing a Webcam Photo on Boot and Sending It via Email

This tutorial explains how to create a Python program that uses OpenCV to take a picture with the computer's webcam at startup, checks network connectivity, constructs an email with the photo attached, and automatically sends it, including setup for Linux auto‑start.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Script for Capturing a Webcam Photo on Boot and Sending It via Email

The article introduces a Python project that captures a webcam photo when the computer boots, then emails the picture to a predefined address, providing a playful way to see who turned on the machine.

Tools : Deepin 15.9 Linux, Python 2.7 or 3.6, OpenCV for camera access, smtplib and email modules for sending mail, and standard libraries such as os, sys, and time.

Approach : 1. Use OpenCV to capture and save a photo. 2. Verify network connectivity; if unavailable, store the image locally. 3. Build an email with the image as an attachment using the email.mime classes. 4. Send the email via smtplib. 5. Configure the script to run automatically at system startup.

Installation and imports : pip3 install opencv-python or pip install opencv-python Import statements:

import cv2
import smtplib
import sys
import os
import time
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Parameter settings (global variables for SMTP configuration and paths):

smtpserver  = 'smtp.163.com'         # SMTP server
username    = '[email protected]'    # Sender email account
password    = '888888888'            # Email password or authorization code
sender      = '[email protected]'    # Sender address
addressee   = '[email protected]'     # Recipient address
exit_count  = 5                      # Maximum retry attempts
path        = os.getcwd()            # Directory to save the photo

Capture photo function:

def getPicture():
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()
    cv2.imwrite(path + '/person.jpg', frame)
    # Close the camera
    cap.release()

Construct email function:

def setMsg():
    msg = MIMEMultipart('mixed')
    msg['Subject'] = '电脑已经启动'
    msg['From'] = '[email protected] <[email protected]>'
    msg['To'] = addressee
    text = "主人,你的电脑已经开机!
照片如下!"
    text_plain = MIMEText(text, 'plain', 'utf-8')
    msg.attach(text_plain)
    sendimagefile = open(path + '/person.jpg', 'rb').read()
    image = MIMEImage(sendimagefile)
    image["Content-Disposition"] = 'attachment; filename="people.png"'
    msg.attach(image)
    return msg.as_string()

Send email function:

def sendEmail(msg):
    smtp = smtplib.SMTP()
    smtp.connect('smtp.163.com')
    smtp.login(username, password)
    smtp.sendmail(sender, addressee, msg)
    smtp.quit()

Network check function (simple ping):

def isLink():
    return os.system('ping -c 4 www.baidu.com')

Main logic :

def main():
    reconnect_times = 0
    while isLink():
        time.sleep(10)
        reconnect_times += 1
        if reconnect_times == exit_count:
            sys.exit()
    getPicture()
    msg = setMsg()
    sendEmail(msg)

Auto‑start configuration : create autoStart.sh with the following commands and add its execution line to /etc/rc.local so the script runs on boot.

cd /home/projects/sendemail
python2 sendEmile.py
./home/projects/sendemail/autoStart.sh

After rebooting, the system will capture a photo, attach it to an email, and send it to the specified recipient, confirming that the computer has started.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAutomationLinuxOpenCVstartupEmail
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.