Python controls your mobile phone camera to take pictures and automatically sends the pictures to the mailbox

foreword

In today's case, I control my camera to take pictures, and send the pictures to my mailbox via email.
To complete today's case, just remember one important point: you need a camera

ideas

  1. Use opencv to call the camera to take a picture and save the image locally
  2. Use the email library to construct the content of the email, and insert the saved image into the content of the email as an attachment
  3. Send mail to specified mailbox using smtplib library

start code

Tool import

import time
import cv2  # pip install opencv-python -i 镜像源网址
from email.mime.image import MIMEImage  # 用来构造邮件内容的库
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib  # 发送邮件

Call the camera and save the picture

For taking pictures, we use the camera of the mobile phone, and the software uses: IP camera (Android), because in the same local area network, open the APP, and the URL that appears inside is the address of the camera

def GetPicture():
    """
    拍照保存图像
    :return:
    """
    # 创建一个窗口
    cv2.namedWindow('camera', 1)
    # 调用摄像头   IP摄像头APP
    video = "http://admin:[email protected]:8081/video"
    cap = cv2.VideoCapture(video)
    while True:
        success, img = cap.read()
        cv2.imshow("camera", img)
        # 按键处理
        key = cv2.waitKey(10)
        if key == 27:
            # esc
            break
        if key == 32:
            # 空格
            fileaname = 'frames.jpg'
            cv2.imwrite(fileaname, img)

    # 释放摄像头
    cap.release()
    # 关闭窗口
    cv2.destroyWindow("camera")

Run the code and the effect will appear

Create a function to set the content of my email

def SetMsg():
    """
    邮件格式设置
    :return:
    """
    msg = MIMEMultipart('mixed')
    # 标题
    msg['Subject'] = '小姐姐照片'
    msg['From'] = sender  # 发送方邮箱
    msg['To'] = receiver  # 接收方邮箱

    # 邮件正文
    text = '你要的小姐姐照片到了,请接收'
    text_plain = MIMEText(text, 'plain', 'utf-8')  # 正文转码
    msg.attach(text_plain)

    # 图片附件
    SendImageFile = open('D:/控制摄像头拍照并发送邮件/frames.jpg', 'rb').read()
    image = MIMEImage(SendImageFile)

    # 将收件人看见的附件照片名称改为people.png.
    image['Content-Disposition'] = 'attachment; filename = "people.png"'
    msg.attach(image)
    return msg.as_string()

Mail port settings

Authorization code can be obtained here

# 授权码
pwd = "******"   # 最好写自己的

# 服务器接口
host = 'smtp.163.com'
port = 25

sender = '[email protected]' # 最好写自己的
receiver = '[email protected]' # 最好写自己的

Send mail function

def SendEmail(msg):
    """
    发送邮件
    :param msg:邮件内容
    :return:
    """
    smtp = smtplib.SMTP()
    smtp.connect(host,port=25)
    smtp.login(sender, pwd)
    smtp.sendmail(sender, receiver, msg)
    time.sleep(2)
    smtp.quit()

encapsulate

if __name__ == '__main__':
    # 1.拍照保存
    GetPicture()
    # 2.设置邮件格式
    msg = SetMsg()
    # 3.发送邮件
    SendEmail(msg)

Run the code to demonstrate the effect

Take a picture first

sent to email

Guess you like

Origin blog.csdn.net/m0_48405781/article/details/124171923