摄影圈子 hqmana.com

Python controls its own mobile camera to take photos, and automatically sends the photos to the mailbox (for learning reference only)

Hey, everyone, good duck ! I'm a panda

Let's finish this simple case with you today ~

Want to complete today's case , Just remember one key point :
You need a camera


Don't talk much , Let's get started

what are you having? python I won't answer the related error report 、 Or source code information / Module installation / Women's clothing bosses are proficient in skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me

 Insert picture description here

Start code

Tool import

import time
import cv2  # pip install opencv-python -i  Mirror source URL 
from email.mime.image import MIMEImage  #  A library for building mail content 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib  #  Send E-mail 

Call the camera , Save the picture

Taking pictures , It's a camera with a mobile phone , The software uses :IP camera ( Android ), Because in the same LAN , open APP, The web address that appears inside is the address of the camera

def GetPicture():
    """  Take pictures and save images  :return: """
    #  Create a window 
    cv2.namedWindow('camera', 1)
    #  Call the camera  IP camera APP
    video = "http://admin:[email protected]:8081/video"
    cap = cv2.VideoCapture(video)
    while True:
        success, img = cap.read()
        cv2.imshow("camera", img)
        #  Press the key to handle 
        key = cv2.waitKey(10)
        if key == 27:
            # esc
            break
        if key == 32:
            #  Space 
            fileaname = 'frames.jpg'
            cv2.imwrite(fileaname, img)

    #  Release camera 
    cap.release()
    #  close window 
    cv2.destroyWindow("camera")

Run code , There will be an effect
 Please add a picture description

Create a function , Set up my email content

def SetMsg():
    """  Format mail  :return: """
    msg = MIMEMultipart('mixed')
    #  title 
    msg['Subject'] = ' Photos of little sister '
    msg['From'] = sender  #  Sender's email 
    msg['To'] = receiver  #  Receiver's email 

    #  Message body 
    text = ' Here comes the picture of your little sister , Please accept '
    text_plain = MIMEText(text, 'plain', 'utf-8')  #  Text transcoding 
    msg.attach(text_plain)

    #  Picture attachment 
    SendImageFile = open('D:/ Control the camera to take pictures and send emails /frames.jpg', 'rb').read()
    image = MIMEImage(SendImageFile)

    #  Change the name of the attached photo seen by the recipient to people.png.
    image['Content-Disposition'] = 'attachment; filename = "people.png"'
    msg.attach(image)
    return msg.as_string()

Mail port settings

The authorization code can be obtained here
 Please add a picture description

#  Authorization code 
pwd = "******"   #  You'd better write your own 

#  Server interface 
host = 'smtp.163.com'
port = 25

sender = '[email protected]' #  You'd better write your own 
receiver = '[email protected]' #  You'd better write your own 

Send mail function

def SendEmail(msg):
    """  Send E-mail  :param msg: Email content  :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. Take photos and save 
    GetPicture()
    # 2. Format the message 
    msg = SetMsg()
    # 3. Send E-mail 
    SendEmail(msg)

Run code , Demonstration effect

Take a picture first
 Please add a picture description
Sent to email
 Please add a picture description
 Please add a picture description

What about? , Is today's case very simple ?

I hope my article can be helpful to you who are learning by yourself ~

I'm a panda , See you in the next article (*◡‿◡)

 Insert picture description here

版权声明
本文为[Panda aiqia rice]所创,转载请带上原文链接,感谢
https://hqmana.com/2022/210/202207290738380114.html

Scroll to Top