这篇文章给大家分享的是有关Python如何实现控制手机电脑拍照并自动发送邮箱的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
本文思路
通过opencv调用摄像头拍照保存图像本地
用email库构造邮件内容,保存的图像以附件形式插入邮件内容
用smtplib库发送邮件到指定邮箱
代码部分
模块导入
import timeimport cv2 from email.mime.image import MIMEImage # 用来构造邮件内容的库from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport smtplib # 发送邮件
授权码(最好写自己的)
pwd = "******"
服务器接口
host = 'smtp.163.com'port = 25sender = '******@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 timeimport cv2 # pip install opencv-python -i 镜像源网址from email.mime.image import MIMEImage # 用来构造邮件内容的库from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport smtplib # 发送邮件# 授权码pwd = "******" # 最好写自己的# 服务器接口host = 'smtp.163.com'port = 25sender = '******@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如何实现控制手机电脑拍照并自动发送邮箱”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!