Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sawtooth tkinter mainloop frame duration?

Trying to animate a sequence of PIL images using tkinter. The graph of my frame durations (ms) looks like this: sawtooth frame duration

Anyone have any idea what could be causing this spiky sawtooth pattern?

Here's a script to reproduce:

from PIL import Image, ImageTk
import Tkinter

import time
import sys

def generate_frames(n):
    """
    keep n under 101 * 101
    """
    out = []
    last_pil = None
    for i in range(n):
        if last_pil:
            pil_image = last_pil.copy()
        else:
            pil_image = Image.new('L', (101, 101), 255)   
        x = i / 101
        y = i % 101
        pil_image.load()[x, y] = 0
        out.append(ImageTk.PhotoImage(pil_image))
        last_pil = pil_image

    return out

def draw():
    FRAME_COUNT =5000

    master = Tkinter.Tk()

    w = Tkinter.Canvas(master, width=302, height=302)
    w.create_rectangle(49, 49, 252, 252)
    w.pack()

    frames = generate_frames(FRAME_COUNT)

    def draw_frame(f, canvas_image):
        print repr(time.time())
        frame = frames[f]
        if canvas_image is None:
            canvas_image = w.create_image((151, 151), image=frame, anchor='center')
        else:
            w.itemconfigure(canvas_image, image=frame)

        w.current_frame = frame  # save a reference
        next_frame = f + 1
        if next_frame < FRAME_COUNT:
            master.after(1, draw_frame, next_frame, canvas_image)
        else:
            sys.exit(0)

    master.after(10, draw_frame, 0, None)
    master.mainloop()


draw()

To see the plot, pipe output through

import sys

last = None
for line in sys.stdin:
    value = float(line.strip()) * 1000
    if last is None:
        pass
    else:
        print (value - last)
    last = value

then through

from matplotlib import pyplot
import sys

X = []
Y = []

for index, line in enumerate(sys.stdin):
    line = line.strip()
    X.append(index)
    Y.append(float(line))

pyplot.plot(X, Y, '-')
pyplot.show()

Making it multi-threaded doesn't help:

enter image description here

class AnimationThread(threading.Thread):

    FRAME_COUNT = 5000

    def __init__(self, canvas):
        threading.Thread.__init__(self)
        self.canvas = canvas
        self.frames = generate_frames(self.FRAME_COUNT)

    def run(self):
        w = self.canvas
        frames = self.frames
        canvas_image = None
        for i in range(self.FRAME_COUNT):
            print repr(time.time())
            frame = frames[i]
            if canvas_image is None:
                canvas_image = w.create_image((151, 151), image=frame, anchor='center')
            else:
                w.itemconfigure(canvas_image, image=frame)
            w.current_frame = frame
            time.sleep(1 * .001)

def draw_threaded():
    FRAME_COUNT = 5000
    master = Tkinter.Tk()

    w = Tkinter.Canvas(master, width=302, height=302)
    w.create_rectangle(49, 49, 252, 252)
    w.pack()

    animation_thread = AnimationThread(w)
    animation_thread.start()

    master.mainloop()

    animation_thread.join()

draw_threaded()
like image 882
sobel Avatar asked Oct 16 '12 03:10

sobel


1 Answers

That closely resembles this kind of interference pattern when competing 60 and 50 Hz samples mingle:

Interference Pattern of competing 60 Hz and 50 Hz samples

(Original Wolfram|Alpha plot)

This is likely caused by having two things at different (but close) refresh rates. It's the same type of thing that happens when you try to film a TV screen and it looks like a black bar keeps moving down the image, or when car wheels appear to rotate backwards around their axles in car commercials. It is essentially an extension of the Moiré Effect.

I don't know whether or not it is caused by video drivers and/or hardware, but it is almost certainly caused by interfering cyclical patterns. It looks a lot like it should be the GC cycle interfering with your for loop (hence the sudden drop in the sawtooth-like wave as memory is freed up and can be allocated)

like image 72
KevinOrr Avatar answered Sep 22 '22 14:09

KevinOrr