In this article, I will tell you how to control your mobile phone’s camera to take photos, and then send the photos to your mailbox by email in python. To implement this, you should use 3 python libraries, they are opencv, email, and smtplib. You can call the camera to take pictures and save the image locally through opencv, and then use the email library to construct the email content, and the saved image is inserted into the email content as an attachment, finally use the smtplib library to send mail to the specified mailbox.
1. How To Use Python OpenCV Library To Call The Mobile Phone Camera To Take Pictures And Save The Image Locally.
- The OpenCV library provides a number of methods to control the camera, take pictures, and save the images locally.
- The cv2.VideoCapture() method calls the camera and initiates a video capture.
- Once the video capture has been started, the cv2.imwrite() method can be used to save the image locally.
- Additionally, the cv2.imshow() method can be used to preview the image before it is saved.
- The following code example uses the OpenCV library to call the camera, take pictures, and save the images locally.
import cv2 # Call camera cap = cv2.VideoCapture(0) # Capture frame-by-frame ret, frame = cap.read() # Save the image locally cv2.imwrite('image.jpg', frame) # Preview the image cv2.imshow('image', frame) # Release the camera cap.release()
2. How To Use Python email Library To Attach Image To Email Content & Use smtplib Library To Send The Email.
- The Python email library provides a number of methods to attach an image to email content.
- The email.mime.image.MIMEImage module can be used to attach an image to an email message.
- Additionally, the email.mime.multipart.MIMEMultipart module can be used to create a message with multiple parts, including an image.
- Finally, the email.mime.base.MIMEBase module can be used to create a base message, which can be used to attach an image.
- The following code example uses the Python email library to attach an image to email content.
# Import the modules import email, smtplib, ssl from email import encoders from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart # Create a multipart message and set headers message = MIMEMultipart() message["From"] = sender_email message["To"] = receiver_email message["Subject"] = "Image Attachment" # Add body to email message.attach(MIMEText(body, "plain")) # Open image file with open(filename, "rb") as attachment: # Add file as application/octet-stream # Email client can usually download this automatically as attachment part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # Encode file in ASCII characters to send by email encoders.encode_base64(part) # Add header as key/value pair to attachment part part.add_header( "Content-Disposition", f"attachment; filename= {filename}", ) # Add attachment to message and convert message to string message.attach(part) text = message.as_string() # Log in to server using secure context and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, text)