邮件发送到时将两张图片以html格式发送,而不是以附件的方式发送.
由于笔记本摄像头启动之后直接拍摄照片的话拍摄出来的图片会是黑乎乎的一片。
做了调整:
添加了一个循环,等摄像头完全开启之后在拍摄照片,这样效果会更好.
添加了time.sleep(240) 就是在电脑开机之后4分钟在进行以下操作.
#author:liangliang
#email:liangliangyy@gmail.com
#blog:http://www.lylinux.org/
#site: www.jb200.com
import time
from VideoCapture import Device
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
from PIL import ImageGrab
from PIL import ImageEnhance
import os
def get_correcttime() :
open_time = time.strftime('%m%d%H%M',time.localtime(time.time()))
return open_time
def get_desktopimg(correcttime):
filename = r"F:/learn/watchcomputer/desktop" + correcttime + ".jpg"
pic = ImageGrab.grab()
pic.save(filename)
print "desktop img saved ok!!!!"
return filename
def get_webcamimg(correcttime):
filename = r"F:/learn/watchcomputer/webcam" + correcttime + ".jpg"
res = (640,480)
cam = Device()
cam.setResolution(res[0],res[1])
brightness = 1.0
contrast = 1.0
i = 0
while i<300:
camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
# filename = "test/"+ str(time.time()) + ".jpg"
i = i + 1
print i
cam.saveSnapshot(filename,timestamp=3, boldfont=1, quality=80)
print "webcam img saved ok!!!!!!"
return filename
def send_img(desktop,webcam):
try:
from_mail = "liangliangyy@gmail.com"
to_mail = "liangliangyy@gmail.com"
msg = MIMEMultipart()
msg['From'] = from_mail
msg['To'] = to_mail
msg['Subject'] = 'my computer'
body = 'test img send'
html_code = '<b><i>the guy who use my computer and the screen shot.</i></b> <img alt="desktop" src="cid:image1" width="683" height="384" /><img alt="webcam" src="cid:image2" />'
print html_code
con = MIMEText(html_code,'html','utf-8')
#msg.attach(con)
#img1 = MIMEImage(file(desktop,'rb').read())
#img2 = MIMEImage(file(webcam,'rb').read())
#msg.attach(img1)
#msg.attach(img2)
msg.attach(con)
img1 = MIMEImage(file(desktop,'rb').read())
img2 = MIMEImage(file(webcam,'rb').read())
img1.add_header('Content-ID','')
img2.add_header('Content-ID','')
msg.attach(img1)
msg.attach(img2)
server = smtplib.SMTP('smtp.gmail.com')
server.docmd('ehol',from_mail)
server.starttls()
server.login(from_mail,'password')
server.sendmail(from_mail,to_mail,msg.as_string())
server.quit()
print "mail send ok!!!!!"
return True
except:
return False
def write_log(issuccess):
log_file_dir = r"F:/learn/watchcomputer/computer.log"
log_flie = file(log_file_dir,'a')
log_flie.write('at '+now_time + ' img saved success! ')
if(issuccess):
log_flie.write('mail send success!!!!!')
else:
log_flie.write('mail send failed.....')
log_flie.write('nn')
log_flie.close()
time.sleep(240)
now_time = get_correcttime()
desktopimg = get_desktopimg(now_time)
webcamimg = get_webcamimg(now_time)
if(os.path.isfile(desktopimg) and os.path.isfile(webcamimg)):
MailIsSend = send_img(desktopimg,webcamimg)
write_log(MailIsSend)
else:
exit(1)
print 'haaa running success !!!!'
exit(1)