用到的素材
素材
链接: https://pan.baidu.com/s/1ZAvHZXNRHEjIRS9qzgONmQ 提取码: i2z4 复制这段内容后打开百度网盘手机App,操作更方便哦
代码
# coding=gbk
from tkinter import *
import cv2
from PIL import Image,ImageTk,ImageDraw,ImageFont
import random
import numpy as np
# 主窗口
window = Tk()
window.title('抽奖程序')
window.geometry('662x285')
bg = Image.open("bg.jpg").resize((662,285))
bg_pic = ImageTk.PhotoImage(bg)
lab = Label(window,image=bg_pic)
lab.pack()
# 单抽函数
def dan():
num = random.randint(1,10)
jin = ['获得三个赞','可以指定下节课\n的上课时间哦~']
zi = ['下节课的编程小\n讲师就是你了','哇哦~\n指定一位同学讲\n作业']
lan = ["ok\n下节课的编程小\n讲师就是你了","哈哈哈哈,现在\n做三个蹲起","今天运气贼棒\n那就多做一题回\n家作业"]
if 1 <= num < 3:
m = '单抽出金.mp4'
n = 'five.jpg'
# 七个字一行,超过七个字要\n换行
w = random.choice(jin)
elif 3 <= num < 6:
m = '单抽出紫.mp4'
n = 'fore.jpg'
w = random.choice(zi)
else:
m = '单抽出蓝.mp4'
n = 'three.jpg'
w = random.choice(lan)
# w2 = Toplevel()
# w2.title('单抽出奇迹')
# w2.geometry('960x540')
cap = cv2.VideoCapture(m)
while True:
ret,frame = cap.read()
if ret:
new = cv2.resize(frame,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR)
cv2.namedWindow('Good luck',0)
cv2.resizeWindow('Good luck',960,540)
cv2.imshow('Good luck',new)
cv2.waitKey(2)
else:
break
cap.release()
## cv2.destroyAllWindows()
# 视频之后接图片
img = cv2.imread(n)
resized = cv2.resize(img,(960,540))
cv2img = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB) # cv2和PIL中颜色的hex码的储存顺序不同
# 将cv2格式转化为PIL格式
pilimg = Image.fromarray(cv2img)
# PIL图片上打印汉字
draw = ImageDraw.Draw(pilimg) # 图片上打印
# 参数1:字体文件路径,参数2:字体大小 ,参数3:编码格式
font = ImageFont.truetype("simsun.ttc", 30, encoding="utf-8")
# 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
draw.text((400, 210), w, (255, 255, 0), font=font)
# PIL图片转cv2 图片
# cv2.cvtColor颜色空间转换函数,第一个参数是需要转换的图片,第二个参数是转换成何种格式,这里是将RGB换成BGR
cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
# 图片写字
# str = w
# font = cv2.FONT_HERSHEY_COMPLEX
# color = (200, 200, 0)
# cv2.putText(resized, str, (300, 300), font, 5, color, 3)
cv2.imshow('Good luck', cv2charimg)
cv2.waitKey(1)
# 十连函数
def shi():
num = random.randint(1, 10)
jin = ['获得三个赞','可以指定下节课\n的上课时间哦~']
zi = ['下节课的编程小\n讲师就是你了','哇哦~\n指定一位同学讲\n作业']
# lan = ['没有抽到哦~下\n次加油']
if 1 <= num < 3:
m = '十连出金.mp4'
n = 'five.jpg'
# 七个字一行,超过七个字要\n换行
w = random.choice(jin)
else:
m = '十连出紫.mp4'
n = 'fore.jpg'
w = random.choice(zi)
# w2 = Toplevel()
# w2.title('单抽出奇迹')
# w2.geometry('960x540')
cap = cv2.VideoCapture(m)
while True:
ret, frame = cap.read()
if ret:
new = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
cv2.namedWindow('Good luck', 0)
cv2.resizeWindow('Good luck', 960, 540)
cv2.imshow('Good luck', new)
cv2.waitKey(1)
else:
break
cap.release()
## cv2.destroyAllWindows()
# 视频之后接图片
img = cv2.imread(n)
resized = cv2.resize(img, (960, 540))
cv2img = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB) # cv2和PIL中颜色的hex码的储存顺序不同
# 将cv2格式转化为PIL格式
pilimg = Image.fromarray(cv2img)
# PIL图片上打印汉字
draw = ImageDraw.Draw(pilimg) # 图片上打印
# 参数1:字体文件路径,参数2:字体大小 ,参数3:编码格式
font = ImageFont.truetype("simsun.ttc", 30, encoding="utf-8")
# 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
draw.text((400, 210), w, (255, 255, 0), font=font)
# PIL图片转cv2 图片
# cv2.cvtColor颜色空间转换函数,第一个参数是需要转换的图片,第二个参数是转换成何种格式,这里是将RGB换成BGR
cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
# 图片写字
# str = w
# font = cv2.FONT_HERSHEY_COMPLEX
# color = (200, 200, 0)
# cv2.putText(resized, str, (300, 300), font, 5, color, 3)
cv2.imshow('Good luck', cv2charimg)
cv2.waitKey(2)
# 按钮
btn1 = Button(window, text="抽一次",bg='gold',font=("微软雅黑", 16),width=10,command=dan)
btn1.place(x=125, y=240)
btn2 = Button(window, text="抽十次",bg='gold',font=("微软雅黑", 16),width=10,command=shi)
btn2.place(x=400, y=240)
mainloop()