在上课时,有时需要显示一个倒计时时钟,让学生做题。 PPT 没有简单有效的方法实现倒计时时钟,参考了多个方案,最终决定采用 GIF 动画来实现。
这样使用起来很简单,只要把事先做好的各个时长的倒计时动画按需拖入 PPT 即可。
下面这个是一分钟的倒计时动画
已生成好了 1~15 分钟倒计时,可以点下面链接下载使用,图片尺寸是 900x700
https://www.jianguoyun.com/p/DVMc_zUQjrLzBRjch-AD
动画是用 Python 代码生成的,如果想改 UI, 可以修改下面代码来重新生成
# -*- coding: utf-8 -*-
from shutil import copyfile
import imageio
import os
from PIL import Image
size = (900, 700)
nums = []
im_num_back = None
for n in range(11):
num_filename = r\'numbers\image%s.png\' % n
im = Image.open(num_filename)
scale = 0.6
scale2 = 0.4
if n == 10:
im = im.resize((int(136 * scale2), int(362 * scale2)))
else:
im = im.resize((int(267 * scale) - 2, int(386 * scale) - 3))
nums.append(im)
im_num_back = Image.open(r\'numbers\image_black.png\')
im_num_back = im_num_back.resize((int(267 * scale) - 2, int(386 * scale) - 3))
def create_clock_png(time_str, filepath):
img = Image.new(\'RGB\', size, color=(68, 84, 106))
x = 186
y_base = 264
for j in range(len(time_str)):
s = time_str[j:j + 1]
if s == \':\':
index = 10
y = y_base + 50
x = x + 20
else:
index = int(s)
y = y_base
im_num = nums[index]
if s != \':\':
img.paste(im_num_back, (x, y), im_num_back)
img.paste(im_num, (x, y), im_num)
if s == \':\':
x = x + 30
else:
x = x + 107
img = img.convert(\'P\', palette=Image.ADAPTIVE, colors=255)
img.save(filepath)
def create_gif(duration, folder, name):
file_path = r\'%s\%s\' % (folder, name)
if not os.path.exists(file_path):
os.makedirs(file_path)
cover_file = file_path + r\'\%s_封面.png\' % name
gif_file = file_path + r\'\%s_动图.gif\' % name
remain = duration
i = 1
for s in range(duration, -1, -1):
second = remain % 60
minute = int((remain - second) / 60)
second_str = str(second).rjust(2, \'0\')
minute_str = str(minute).rjust(2, \'0\')
time_str = \'%s:%s\' % (minute_str, second_str)
print(time_str)
temp_file = r\'temp\image_%s.png\' % i
create_clock_png(time_str, temp_file)
if i == 1:
copyfile(temp_file, cover_file)
remain = remain - 1
i = i + 1
filenames = []
for i in range(1, duration + 2):
filename = r\'temp\image_%s.png\' % i
filenames.append(imageio.imread(filename))
imageio.mimsave(gif_file, filenames, duration=1, loop=1)
def start():
for k in range(1, 16):
folder = \'gif\'
name = \'%s分钟\' % k
duration = k * 60
create_gif(duration, folder, name)
if __name__ == \'__main__\':
start()
exit()
时间匆忙,代码没有写注释,也没有优化。等有时间了再优化
代码中用到了一些图片文件,可以下载代码打包版本,里面有。代码打包下载地址:
https://www.jianguoyun.com/p/DZyUuOsQjrLzBRjniOAD
如果下载或使用中遇到问题,可加 QQ 群交流,群号:599320555