如何将相机拍摄的图像发送到emai

2024-04-20 03:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序可以检测人脸并将其保存到文件夹中。我想把这些图片发送到一个电子邮件id,但我不知道怎么做。你知道吗

以下是保存图像的代码:

import cv2


#import the cascade for face detection
FaceClassifier =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# access the webcam (every webcam has 
capture = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame

    ret, frame = capture.read()
    if not capture:
        print ("Error opening webcam device")
        sys.exit(1)


    # to detect faces in video
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = FaceClassifier.detectMultiScale(gray, 1.3, 5)

    # Resize Image 
    minisize = (frame.shape[1],frame.shape[0])
    miniframe = cv2.resize(frame, minisize)
    # Store detected frames in variable name faces
    faces =  FaceClassifier.detectMultiScale(miniframe)
   # Draw rectangle 
    for f in faces:
        x, y, w, h = [ v for v in f ]
        cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255))
        #Save just the rectangle faces in SubRecFaces
        sub_face = frame[y:y+h, x:x+w]
        FaceFileName = "faces/face_" + str(y) + ".jpg"
        cv2.imwrite(FaceFileName, sub_face)
        #Display the image 
        cv2.imshow('Result',frame)
        cv2.waitKey(180)
        break

    # When everything is done, release the capture

img.release()
cv2.destroyAllWindows()

我不知道如何将保存的图像发送到电子邮件id。请帮助!你知道吗


Tags: thein图像importidfor电子邮件cv2
2条回答

@Arpit说一定是smtp。使用smtplib有一个简单的方法:

# Import smtplib for the actual sending function
import smtplib

# And imghdr to find the types of our images
import imghdr

# Here are the email package modules we'll need
from email.message import EmailMessage

# Create the container email message.
msg = EmailMessage()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = ', '.join(family)
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'

# Open the files in binary mode.  Use imghdr to figure out the
# MIME subtype for each specific image.
for file in pngfiles:
    with open(file, 'rb') as fp:
        img_data = fp.read()
    msg.add_attachment(img_data, maintype='image',
                                 subtype=imghdr.what(None, img_data))

# Send the email via our own SMTP server.
with smtplib.SMTP('localhost') as s:
    s.send_message(msg)

从这里开始: SMTPLIB

我们用SendGrid来做这个。它是一个基于web的服务,并且有一个python模块与之接口。你知道吗

SendGrid

SendGrid Python Module

下面是一些附加PDF的示例代码,因此基本上您只需将FileType调用更改为图像的任何内容,并将FileName调用更改为图像。你知道吗

import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
    Mail, Attachment, FileContent, FileName,
    FileType, Disposition, ContentId)
try:
    # Python 3
    import urllib.request as urllib
except ImportError:
    # Python 2
    import urllib2 as urllib

import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='from_email@example.com',
    to_emails='to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
file_path = 'example.pdf'
with open(file_path, 'rb') as f:
    data = f.read()
    f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

相关问题 更多 >