首頁 > 軟體

Python實現控制手機電腦拍照並自動傳送郵箱

2022-02-19 10:00:23

介紹

兄弟們,這個是正經的教學,不要拿來亂用,可以自己用自己的電腦或者手機試,但是別搞別人,懂的都懂!

本文思路

1.通過opencv呼叫攝像頭拍照儲存影象本地

2.用email庫構造郵件內容,儲存的影象以附件形式插入郵件內容

3.用smtplib庫傳送郵件到指定郵箱

視訊講解

用python控制手機/電腦自動拍照並行送郵箱

程式碼部分

模組匯入

import time
import cv2   
from email.mime.image import MIMEImage  # 用來構造郵件內容的庫
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib  # 傳送郵件

授權碼(最好寫自己的)

pwd = "******"

伺服器介面

host = 'smtp.163.com'
port = 25

sender = '******@163.com' # 最好寫自己的
receiver = '******@qq.com' # 最好寫自己的

拍照儲存影象

def GetPicture():
    """
    :return:
    """
    # 建立一個視窗
    cv2.namedWindow('camera', 1)
    # 呼叫攝像頭   IP攝像頭APP
    video = "http://admin:admin@192.168.3.32:8081/video"
    cap = cv2.VideoCapture(video)
    while True:
        success, img = cap.read()
        cv2.imshow("camera", img)
        # 按鍵處理
        key = cv2.waitKey(10)
        if key == 27:
            # esc
            break
        if key == 32:
            # 空格
            fileaname = 'frames.jpg'
            cv2.imwrite(fileaname, img)

    # 釋放攝像頭
    cap.release()
    # 關閉視窗
    cv2.destroyWindow("camera")

郵件格式設定

def SetMsg():
    """
    :return:
    """
    msg = MIMEMultipart('mixed')
    # 標題
    msg['Subject'] = '小姐姐照片'
    msg['From'] = sender  # 傳送方郵箱
    msg['To'] = receiver  # 接收方郵箱

    # 郵件正文
    text = '你要的小姐姐照片到了,請接收'
    text_plain = MIMEText(text, 'plain', 'utf-8')  # 正文轉碼
    msg.attach(text_plain)

    # 圖片附件
    SendImageFile = open('D:/控制攝像頭拍照並行送郵件/frames.jpg', 'rb').read()
    image = MIMEImage(SendImageFile)

    # 將收件人看見的附件照片名稱改為people.png.
    image['Content-Disposition'] = 'attachment; filename = "people.png"'
    msg.attach(image)
    return msg.as_string()

傳送郵件

def SendEmail(msg):
    """
    :param msg:郵件內容
    :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.拍照儲存
    GetPicture()
    # 2.設定郵件格式
    msg = SetMsg()
    # 3.傳送郵件
    SendEmail(msg)

全部程式碼

import time
import cv2  # pip install opencv-python -i 映象源網址
from email.mime.image import MIMEImage  # 用來構造郵件內容的庫
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib  # 傳送郵件

# 授權碼
pwd = "******"   # 最好寫自己的

# 伺服器介面
host = 'smtp.163.com'
port = 25

sender = '******@163.com' # 最好寫自己的
receiver = '****@qq.com' # 最好寫自己的


def GetPicture():

    # 建立一個視窗
    cv2.namedWindow('camera', 1)
    # 呼叫攝像頭   IP攝像頭APP
    video = "http://admin:admin@192.168.3.32:8081/video"
    cap = cv2.VideoCapture(video)
    while True:
        success, img = cap.read()
        cv2.imshow("camera", img)
        # 按鍵處理
        key = cv2.waitKey(10)
        if key == 27:
            # esc
            break
        if key == 32:
            # 空格
            fileaname = 'frames.jpg'
            cv2.imwrite(fileaname, img)

    # 釋放攝像頭
    cap.release()
    # 關閉視窗
    cv2.destroyWindow("camera")


def SetMsg():

    msg = MIMEMultipart('mixed')
    # 標題
    msg['Subject'] = '小姐姐照片'
    msg['From'] = sender  # 傳送方郵箱
    msg['To'] = receiver  # 接收方郵箱

    # 郵件正文
    text = '你要的小姐姐照片到了,請接收'
    text_plain = MIMEText(text, 'plain', 'utf-8')  # 正文轉碼
    msg.attach(text_plain)

    # 圖片附件
    SendImageFile = open('D:/控制攝像頭拍照並行送郵件/frames.jpg', 'rb').read()
    image = MIMEImage(SendImageFile)

    # 將收件人看見的附件照片名稱改為people.png.
    image['Content-Disposition'] = 'attachment; filename = "people.png"'
    msg.attach(image)
    return msg.as_string()


def SendEmail(msg):

    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.拍照儲存
    GetPicture()
    # 2.設定郵件格式
    msg = SetMsg()
    # 3.傳送郵件
    SendEmail(msg)

到此這篇關於Python實現控制手機電腦拍照並自動傳送郵箱的文章就介紹到這了,更多相關Python控制手機電腦拍照內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com