在上課時,有時需要顯示一個倒數計時時鐘,讓學生做題。 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