Python 使用PIL.Image制作運(yùn)動(dòng)小人的動(dòng)態(tài)圖思路詳解
準(zhǔn)備材料:
圖片img.png 大?。?04x165

制作思路:
把圖片拆分成12等分,每幀大?。?7x165;連續(xù)讀取和播放就會(huì)形成動(dòng)態(tài)圖像。
源代碼:
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='動(dòng)作/停止').place(x=170,y=275)
root.mainloop()
運(yùn)行效果:

到此這篇關(guān)于Python 使用PIL.Image制作一個(gè)運(yùn)動(dòng)小人的動(dòng)態(tài)圖的文章就介紹到這了,更多相關(guān)Python PIL.Image動(dòng)態(tài)圖內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信