【问题标题】:How to send image taken by camera to email如何将相机拍摄的图像发送到电子邮件
【发布时间】:2019-11-26 13:44:36
【问题描述】:

我有一个程序可以检测人脸并将其保存到文件夹中。我想将这些图像发送到电子邮件 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。请帮忙!

【问题讨论】:

标签: python smtp


【解决方案1】:

我们为此使用 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)

【讨论】:

    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多