Screenshot of Image

I have a problem when taking screenshots with PIL ImageGrab and saving the pictures.

Ironically it works when my monitor is connected directly on the mainboard, but when i connect it to the GPU i just get black images.

This is my code:

from psychopy import visual, gui, core
import os, sys, wx
from PIL import ImageGrab

expName = os.path.basename(sys.argv[0]).split('.')[0]
info = {'ProbandInnencode': ''}
dialogObj = gui.DlgFromDict(info, title="Test Script", order=["ProbandInnencode"])

if dialogObj.OK:
    expInfo = dialogObj.data
else:
    core.quit()

app = wx.App(False)
sizeX = wx.GetDisplaySize()[0]
sizeY = wx.GetDisplaySize()[1]
win = visual.Window(screen=2, size=[sizeX, sizeY], fullscr=True, color="black")
image = visual.ImageStim(win=win, name='image', units='pix', image='Stimuli/Test.PNG', pos=(0, 0), size=(250,250), color="white")
brush = visual.Brush(win=win, name='brush', lineWidth=5.0, lineColor="black", buttonRequired=True)
fix = visual.TextStim(win=win, name='fix', height=0.2, text='+')
imX = image.size[0]
imY = image.size[1]
for i in range(3):
    for frameN in range(30):
        fix.draw()
        win.flip()
    for frameN in range(300):
        image.draw()
        brush.draw()
        win.flip()
    im = ImageGrab.grab(bbox=(((((sizeX-imX) / 2))), ((((sizeY-imY) / 2))), ((((sizeX-imX) / 2)) + imY),((((sizeY - imY) / 2)) + imY)))
    im.save("data/Item_"+str(i)+".PNG")
    brush.reset()
    for frameN in range(30):
        win.flip()

Any ideas what could cause this behavior?

Kind regards,
Dennis

Difficult to say why this isn’t working, but in the latest release you can get a screenshot of the current window using win.screenshot and could then crop it as desired? The output is a PIL image so you can crop it programmatically with PIL functions

How can I use win.screenshot? I don’t find it in the documentation and screenshot = win.screenshot() doesn’t work. Version is 2021.2.2

Kind regards,
Dennis

Nevermind, win.getMovieFrame() also works like a charm. :wink:

Hello, I have met the same issue, but it does not work online. I am wondering do you run it locally or online for save image function?