Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_animate_gif.py # https://media.giphy.com/media/UuYo0xOTHOvTDEkRdv/giphy.gif
- from Tkinter import *
- from PIL import Image, ImageTk
- from tkFileDialog import askopenfilename
- import time
- import os
- root = Tk()
- xx = 600
- yy = 600
- canvas = Canvas(root, width=xx, height=yy)
- canvas.grid()
- filename = askopenfilename(filetypes=[('gif files', '.gif')]) # needs to be placed after pack()
- img_data = Image.open(filename)
- # print list(img_data.getdata().convert('RGB')) # ... or RGBA
- ww,hh = img_data.size
- img = Image.new("RGB",(ww,hh))
- seq = []
- label = Label(root, image=None)
- label.grid(row=0, column=0)
- try:
- while 1:
- seq.append(img_data.copy())
- img_data.seek(len(seq)) # skip to next frame
- except EOFError:
- pass # task done
- L = len(seq)
- print 'number of frames:',L
- try:
- delay = img_data.info['duration']
- except KeyError:
- delay = 100
- print 'ms delay:',delay
- frames = []
- for image in seq:
- frame = image.convert('RGBA')
- frames.append(ImageTk.PhotoImage(frame))
- def play(i):
- label.configure(image=frames[i])
- label.update()
- i += 1
- if i == L:
- i = 0
- root.after(delay, play, i)
- root.after(0, play, 0)
- root.mainloop()
- # canvas.delete('gif'+str(p)) will most likely cause too much flickering...
- # ... or without... might significantly keep reducing the process speed
- '''
- L = len(frames)
- buffer = 0
- while 1:
- if time.time() > ta:
- ta = time.time()+1.5
- p = buffer
- buffer = (1,0)[buffer]
- canvas.create_image(1, 1, anchor=NW, image=frames[i%L], tag='gif'+str(buffer))
- canvas.delete('gif'+str(p))
- i = (i+1)%L
- canvas.create_image(1, 1, anchor=NW, image=frames[i%L], tag='gif'+str(buffer))
- canvas.update()
- while time.time() < tb:
- pass
- tb = time.time()+0.01
- '''
Add Comment
Please, Sign In to add comment