3

I am trying to build a GUI that displays sequence of images as videos. The images are numpy arrays.

The code is working when I try to display one image at a time but it crashes when I try to run them as a sequence.

The code:

from tkinter import *
from scipy.io import loadmat
from PIL import ImageTk, Image
import time
data = loadmat('DepthFrames.mat')['DepthFrames'].squeeze(axis=0)
print(data.shape)
counter = 0
root = Tk()
image = ImageTk.PhotoImage(image = Image.fromarray(data[counter]))
root.title("WUDU VIDEOS LABEL TOOL")
myLabel = Label(root, image = image)
myLabel.grid(row = 0)
def changeImg():
    global counter
    counter +=1
    print(counter)
    image = ImageTk.PhotoImage(image = Image.fromarray(data[counter]))
    myLabel.configure(image = image)
    myLabel.image = image

def playVideo():
    for i in range(10):
        image = ImageTk.PhotoImage(image = Image.fromarray(data[i]))
        myLabel.configure(image = image)
        myLabel.image = image
        time.sleep(0.03333)
my_Button = Button(text = "Play video",command = playVideo)
my_Button.grid(row = 1)
root.mainloop()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

1

time.sleep blocks the main thread of tkinter. Your code will freeze the GUI until the for loop is completed and the image will be shown as the last image. For more details, see this post.

You need to use the after method. Something like this:

def playVideo(frame=0):
    try:
        image = ImageTk.PhotoImage(image = Image.fromarray(data[frame]))
    except IndexError:
        return
    myLabel.configure(image = image)
    myLabel.image = image
    root.after(33, playVideo, frame+1)
Henry Yik
  • 22,275
  • 4
  • 18
  • 40