Let's finish this simple case with you today ~
Want to complete today's case , Just remember one key point :
You need a camera
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 skillsYou can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me
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
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()
The authorization code can be obtained here
# 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
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()
if __name__ == '__main__':
# 1. Take photos and save
GetPicture()
# 2. Format the message
msg = SetMsg()
# 3. Send E-mail
SendEmail(msg)
Take a picture first
Sent to email