screenshots during a psychopy experiment

  • Last Update :
  • Techknowledgy :

Use win.getMovieFrame and win.saveMovieFrames as others have suggested. You don't need the visual.BufferImageStim. You'd likely end up with a loop over conditions When you finish your script. I'd be taking the screenshots as the actual experiment is running rather than "simulating" beforehand. It ensures that your screenshots are an accurate depiction of what's actually going on during the experiment - also if you'd make mistakes and drew things incorrectly :-) Of course, if the purpose of the screenshots are purely for the documentation, do remove/outcomment those lines as you run the actual experiment to improve performance.

# Loop through trials.You may organize them using ``
data.TrialHandler``
or generate them yourself.
for trial in myTrialList:
   # Draw whatever you need, probably dependent on the condition.E.g.:
   if trial['condition'] == 'right':
   rightimage.draw()
else:
   leftimage.draw()
fixation.draw()

# Show your stimulus
win.flip()

# Save screenshot.Maybe outcomment these line during production.
win.getMovieFrame() # Defaults to front buffer, I.e.what 's on screen now.
win.saveMovieFrames('screenshot' + trial['condition']) # save with a descriptive and unique filename..

I can't test this as I don't have PyschoPy set up on my current computer, but using Window.getMovieFrame() and Window.saveMovieFrames() should get you to where you need to be, e.g.:

screenshot = visual.BufferImageStim(win, stim = stimlist, rect = rect)
# rect is the screen rectangle to grab, (-1, 1, 1, -1) is whole screen
# as a list of the edges: Left Top Right Bottom, in norm units.
# screenshot is currently on the 'back'
buffer as we haven 't flipped yet
win.getMovieFrame(buffer = 'back')
win.saveMovieFrames('stimuli.png')

I've had trouble using all the answers provided that require win.flip() whilst using PsychoPy builder. Below is the solution that worked for me and uses PIL:

from PIL
import ImageGrab
import os

os.makedirs("./data/" + expInfo['participant'], exist_ok = True)
output_image_name = "./data/" + expInfo['participant'] + "/" + str(datetime.datetime.now()).replace("-", "_").replace(" ", "_").replace(".", "_").replace(":", "_") + ".png"
im = ImageGrab.grab()
im.save(output_image_name, 'png')

Suggestion : 2

We can save screenshots easily in psychopy via a two-step process. First, once we have flipped our window into a state that we would like to save, we can use the command win.getMovieFrame() to tell psychopy to save the current state of the window. Then, we use the command win.saveMovieFrames(img_path) to save this state to a file on disk, where img_path is the name of the file that we want to save it to. For example:,Finally, we will consider how we can take a “screenshot” of what we have drawn using psychopy. This is often a useful procedure, as it allows us to make figures in articles and reports on our research that give a sense of what stimuli participants were viewing.,You may notice in the above examples that the “background” in the image we display seems to match the colour of our window. This is because the image file has embedded in it the desired “transparency” of each pixel, which is respected by psychopy. We can see this more clearly if we draw the image after we have drawn a rectangle:,While most vision science experiments involve stimuli that are generated rather than those that are loaded from images, there are still situations in which drawing images from files that are stored on disk is required. In psychopy, we can use ImageStim.

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
   size = [400, 400],
   units = "pix",
   fullscr = False
)

img = psychopy.visual.ImageStim(
   win = win,
   image = "UNSW.png",
   units = "pix"
)

img.draw()

win.flip()

psychopy.event.waitKeys()
import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
   size = [400, 400],
   units = "pix",
   fullscr = False
)

img = psychopy.visual.ImageStim(
   win = win,
   image = "UNSW.png",
   units = "pix"
)

size_x = img.size[0]
size_y = img.size[1]

img.size = [size_x * 1.5, size_y * 1.5]

img.draw()

win.flip()

psychopy.event.waitKeys()

win.close()
import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
   size = [400, 400],
   units = "pix",
   fullscr = False
)

rect = psychopy.visual.Rect(
   win = win,
   width = 200,
   height = 100,
   pos = [0, -50],
   fillColor = [1] * 3
)

rect.draw()

img = psychopy.visual.ImageStim(
   win = win,
   image = "UNSW.png",
   units = "pix"
)

img.draw()

win.flip()

psychopy.event.waitKeys()

win.close()
import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
   size = [400, 400],
   units = "pix",
   fullscr = False
)

rect = psychopy.visual.Rect(
   win = win,
   width = 200,
   height = 100,
   pos = [0, -50],
   fillColor = [1] * 3
)

rect.draw()

img = psychopy.visual.ImageStim(
   win = win,
   image = "UNSW.png",
   units = "pix"
)

img.opacity = 0.5

img.draw()

win.flip()

psychopy.event.waitKeys()

win.close()
import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
   size = [400, 400],
   units = "pix",
   fullscr = False
)

rect = psychopy.visual.Rect(
   win = win,
   width = 200,
   height = 100,
   pos = [0, -50],
   fillColor = [1] * 3
)

rect.draw()

img = psychopy.visual.ImageStim(
   win = win,
   image = "UNSW.png",
   units = "pix"
)

img.opacity = 0.5

img.draw()

win.flip()

win.getMovieFrame()
win.saveMovieFrames("unsw_logo_blend_example.png")

psychopy.event.waitKeys()

win.close()

Suggestion : 3

I am trying to capture a screenshot during my psychophysical task. I have a cross of a cross followed by 2 faces to the left and right of the screen and then a period. I just want a screenshot of the 1st time period when these two faces appear on the screen. There are 10 different pairs of faces in the subroutine and the procedure is cyclically executed 3 times. Ideally, I would like to have 30 images saved on my computer using this code. I have below code:,I can't verify this as I don't have PyschoPy configured on my current computer, but using Window.getMovieFrame() it Window.saveMovieFrames() should get you where you need to be, e.g .:,Hiding address bar on mobile when scrolling child element with overflow: auto - css,Original error JavaScript sourcemap 404 as a source not available on the web - javascript


# Loop through trials.You may organize them using ``
data.TrialHandler``
or generate them yourself.
for trial in myTrialList:
   # Draw whatever you need, probably dependent on the condition.E.g.:
   if trial['condition'] == 'right':
   rightimage.draw()
else:
   leftimage.draw()
fixation.draw()

# Show your stimulus
win.flip()

# Save screenshot.Maybe outcomment these line during production.
win.getMovieFrame() # Defaults to front buffer, I.e.what on screen now.
win.saveMovieFrames('screenshot' + trial['condition']) # save with a descriptive and unique filename..

(adsbygoogle = window.adsbygoogle || []).push({});

screenshot = visual.BufferImageStim(win, stim = stimlist, rect = rect)
# rect is the screen rectangle to grab, (-1, 1, 1, -1) is whole screen
# as a list of the edges: Left Top Right Bottom, in norm units.
# screenshot is currently on the 'back'
buffer as we haven 't flipped yet
win.getMovieFrame(buffer = 'back')
win.saveMovieFrames('stimuli.png')

(adsbygoogle = window.adsbygoogle || []).push({});