Extracting Window Movie Frame without Creating Window

I want to use PsychoPy to generate stimuli (DotStim, specifically) and then subsequently extract the sequence of PIL images. I am currently instantiating a window, calling dot_stim.draw() and then calling window.getMovieFrame(), but this still requires having a window that pop ups every time the window is instantiated. Is there a way I can extract the sequence of PIL images of my stimuli without instantiating a window or without having the window be visually displayed?

Thank you!

You should be able to do it without the window being displayed using something like xvfb-run.

For example, if this is grating_cap.py:

import psychopy.visual

win = psychopy.visual.Window((400, 400),fullscr=False)

grating = psychopy.visual.GratingStim(win=win)

grating.draw()

win.getMovieFrame(buffer="back")

cap_path = # put filename to save here

win.saveMovieFrames(cap_path)

win.close()

then you should be able to run it without seeing a window with:

xvfb-run grating_cap.py

N.B. it doesn’t seem to work for me (captured image is all black) when capturing the window after flipping, but works before flipping with buffer="back".

Is there another approach that doesn’t require writing the frames to disk? I’d like to immediately use the frames within the same python script.

I’ve now tried and failed to get either pyvirtualdisplay or xvfbwrapper to work. I’ll keep googling and trying.

It seems like editing psychopy.visual.window.py to pass visible=False to pyglet.window.Window works, but this is hacky and I don’t like modifying a library’s code. Is there another way?

You aren’t required to write the frames to disk; getMovieFrame returns a PIL image.