Grabing image content from window

Dear all,

I want to save the raw pixel representation of my presented stimuli. To make the stimuli, I use GratingStim with the appropriate parameters. After drawing the content to the screen and flipping the window, I would like to extract the matrix representation containing the rgb values for all individual pixels. In psuedo-code something like this:

# Create the variables
window = visual.window(...)
grating = visual.GratingStim(...)
grating.draw()
window.flip()

# And then something like..
image = grating.getPixelValues()
# Or maybe?
image = window.getPixelValues()

This is similar to PsychToolbox’ Screen(window, “GetImage”). How can one achieve this, or is it possible at all in Psychopy?

Many thanks!
Jordy

Hi Jordy,
It’s an interesting question so I experimented a little bit myself. It seems, you can easily get your displayed frame as PIL.image and save it with

win._getFrame().save('myimage.png')

with win being an instance of psychopy.visual.Window.

I hope this helps.
Best,
Robin

2 Likes

Hi Schubisu,

Amazing! Thanks a million! The solution I was searching for was just the following:

# Grabe image content as matrix, i.e. its pixel values (rgb)
image = np.array(window._getFrame())

Issue solved!