gpt4 book ai didi

python - 锯齿 tkinter 主循环帧持续时间?

转载 作者:太空狗 更新时间:2023-10-29 17:51:09 28 4
gpt4 key购买 nike

尝试使用 tkinter 为一系列 PIL 图像制作动画。我的帧持续时间 (ms) 的图表如下所示: sawtooth frame duration

有人知道是什么导致了这种尖尖的锯齿图案吗?

这是一个重现的脚本:

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()

要查看绘图,请通过管道输出

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

然后通过

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()

使其成为多线程没有帮助:

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()

最佳答案

当 60 赫兹和 50 赫兹的竞争样本混合时,这与这种干涉图非常相似:

Interference Pattern of competing 60 Hz and 50 Hz samples

(Original Wolfram|Alpha plot)

这可能是由于两个事物的刷新率不同(但接近)所致。这与当您尝试拍摄电视屏幕时发生的相同类型的事情,它看起来像一个黑条一直向下移动图像,或者当汽车车轮似乎围绕其轴向后旋转时在汽车广告中发生。它本质上是 Moiré Effect 的扩展.

我不知道它是否是由视频驱动程序和/或硬件引起的,但几乎可以肯定是由干扰循环模式引起的。看起来应该是 GC 循环干扰了您的 for 循环(因此随着内存被释放并可以分配,锯齿波突然下降)

关于python - 锯齿 tkinter 主循环帧持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907086/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com