准备材料:
图片img.png 大小:804x165
制作思路:
把图片拆分成12等分,每帧大小:67x165;连续读取和播放就会形成动态图像。
源代码:
import tkinter as tk
from PIL import Image,ImageTk
from time import sleep
flag = False
def pause():
global flag
flag = not flag
while flag:
doing()
def doing():
global flag
while flag:
for i in range(12):
if not flag:break
box = f_out.crop((i*67,0,i*67+67,164))
img = ImageTk.PhotoImage(image=box)
gif = cv.create_image(180,135,image=img)
cv.update()
sleep(0.2)
root = tk.Tk()
root.geometry('400x320')
cv = tk.Canvas(root, width=350, height=260, bg='lightgray')
cv.pack()
f_in = 'd:\\img.png'
f_out = Image.open(f_in)
box = f_out.crop((0,0,67,164))
img = ImageTk.PhotoImage(image=box)
gif = cv.create_image(180,135,image=img)
tk.Button(root,command=pause,text='动作/停止').place(x=170,y=275)
root.mainloop()
运行效果:
?
(本篇完) ?
学习交流 Python 的群二维码:?http://qr01.cn/FHYKEa
|