【问题标题】:Generating animation from image sequence in PsychoPy?从 PsychoPy 中的图像序列生成动画?
【发布时间】:2017-01-05 18:53:46
【问题描述】:

我是 PsychoPy 的新手,之前已经使用 Pygame 工作了几个月(我切换到能够在多个屏幕上呈现刺激)。

我试图弄清楚如何使用 PsychoPy 来显示使用一系列图像创建的动画。我之前在 Pygame 中实现了这一点,方法是将整个图像序列保存在一个大的 png 文件(spritesheet)中,然后每帧只翻转该图像的一部分(例如 480 x 480 像素),同时移动到下一个相同大小的下一帧中的图像部分。这大致就是我的代码在 Pygame 中的样子。我真的很想知道是否有一种在 PsychoPy 中生成动画的等效方法,方法是只选择要在每一帧中显示的图像的一部分。到目前为止,谷歌搜索还没有提供任何答案!

gameDisplay=pygame.display.set_mode((800, 480))

sequence=pygame.image.load('C:\Users\...\image_sequence.png') 
#This image contains 10 images in a row which I cycle through to get an  animation

image_width=480
image_height=480

start=time.time()
frame_count=0
refresh=0

while time.time()<=start+15:

    gameDisplay.blit(sequence,(160,0),(frame_count*image_width,0,image_width,image_height)) 

    if time.time()>= start+(refresh*0.25): #Flip a new image say every 250 msec

        pygame.display.update()
        frame_count+=1 
        refresh+=1

    if frame_count ==10:
        frame_count=0

【问题讨论】:

  • 这并不能直接回答您的问题,但是如果您想要类似于支持多显示器的 pygame 的东西,我相信 pysdl2 可能适合您。它应该支持多个窗口,因为它是基于sdl2的,而pygame是基于sdl的。

标签: python animation psychopy


【解决方案1】:

您可以使用方形光圈来限制可见内容,然后移动图像。所以像这样的东西(未经测试,但可以给你一些想法):

from psychopy import visual
win = visual.Window(units='pix')  # easiest to use pixels as unit
aperture = visual.Aperture(win, shape='rect', size=(480, 480))
image = visual.ImageStim('C:\Users\...\image_sequence.png')

# Move through x positions
for x in range(10):
    image.pos = [(-10.0/2*+0.5+x)*480, 0]  # not sure this is right, but it should move through the x-positions
    image.draw()
    win.flip()

如果你有原始图像,我认为只按顺序显示原始图像会更简单。

import glob
from psychopy import visual
image_names = glob.glob('C:\Users\...\*.png')

# Create psychopy objects
win = visual.Window()
image_stims = [visual.ImageStim(win, image) for image in image_names]

# Display images one by one
for image in image_stims:
    image.draw()
    win.flip()
    # add more flips here if you want a lower frame rate

也许在运行时加载它们而不丢帧的速度甚至足够快,这将简化代码并减少加载内存:

# Imports, glob, and win here
# Create an ImageStim and update the image each frame
stim = visual.ImageStim(win)
for name in image_names:
    stim.image = name
    stim.draw()
    win.flip()

【讨论】:

    【解决方案2】:

    实际上,给定一个 spritesheet,您也许可以使用 GratingStim 做一些时髦且更高效的事情。这会将图像加载为纹理,然后允许您设置该纹理的空间频率 (sf) 和相位。如果 1.0/sf(在两个维度上)小于刺激的宽度(在两个维度上),则只会显示纹理的一部分,并且相位决定了哪个部分将是。它不是为此目的而设计的——它通常用于创建一个以上的纹理循环,不少于一个——但我认为它会起作用。

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多