psychopy.org | Reference | Downloads | Github

How to get two or more images to display in the same window

Hello!
I have this code through which I’m aiming to show four different images at a time on a window.
from future import division

from psychopy import visual, core, event
import numpy as np

Create a window to draw in

win = visual.Window(monitor=‘testMonitor’, units =‘norm’, screen=0)

instr = visual.TextStim(win,
units=‘norm’, height = 0.1, text=‘press any key to begin’)

im = visual.ImageStim(win, image=‘wolf.jpg’)

mouse = event.Mouse()

Show the instruction

instr.draw()
win.flip()
event.waitKeys()

im_positions= {0:(-0.5, -0.5),
1:(0.5, 0.5),
2:(-0.5, 0.5),
3:(0.5, -0.5)}

show image

#im.draw()
np.random.randint(4)

win.flip()
clicks = []
while True:

for i in range(4): 
    im.pos = im_positions[i]
    im.draw()

buttons = mouse.getPressed()
pos = mouse.getPos()

if np.any(np.array(buttons)):
    print(buttons)
    
    print(pos)
    
    if pos[0] < 0.1 and pos[0] > -0.1:
        clicks.append(pos)
        print(pos)
        break
    
#core.wait(0.1)
win.flip()

win.close()

core.quit()

I’m having a hard time coding the extra three image files: should I name each file as
im1 = visual.ImageStim(win, image=‘wolf.jpg’)
im2 = visual.ImageStim(win, image=‘duck.jpg’) and so forth?

:slight_smile: any tips would be super helpful!

hi @Vrnc

In future, it would be best if you could use backticks around all of your code, to help with readability.

You could indeed just define all the images you need, and draw them before every win.flip() call. For example:

from psychopy import visual, core, event
import numpy as np

# Createw window
win = visual.Window(monitor='testMonitor', units ='norm', screen = 0)

# Create images
instr = visual.TextStim(win, units='norm', height = 0.1, text = 'Press any key to begin')
# im = visual.ImageStim(win, image='wolf.jpg')
# im1 = visual.ImageStim(win, image='pig.jpg')
# im2 = visual.ImageStim(win, image='duck.jpg')
# im2 = visual.ImageStim(win, image='horse.jpg')
im = visual.ShapeStim(win)
im1 = visual.ShapeStim(win)
im2 = visual.ShapeStim(win)
im3 = visual.ShapeStim(win)
mouse = event.Mouse()

# Image parameters
im_positions = {0: (-0.5, -0.5),
                1: (0.5, 0.5),
                2: (-0.5, 0.5),
                3: (0.5, -0.5)}
# Create list of position keys for shuffling on every trial
imKeys = np.array(list(im_positions.keys()))

# Instructions
continueRoutine = True
while continueRoutine:
    instr.draw()
    win.flip()
    if event.getKeys():
        continueRoutine = False

# Start trial
# Number of trials? We shall start with 4
# How long is each trial? 1 second? So, 60 frams

continueRoutine = True
clicks = []
for trials in range(4):
    # Set position before each trial
    np.random.shuffle(imKeys)
    im.pos=(im_positions[imKeys[0]])
    im1.pos=(im_positions[imKeys[1]])
    im2.pos=(im_positions[imKeys[2]])
    im3.pos=(im_positions[imKeys[3]])
    while continueRoutine:
        # draw image
        im.draw()
        im1.draw()
        im2.draw()
        im3.draw()

        buttons = mouse.getPressed()
        pos = mouse.getPos()

        if np.any(np.array(buttons)):
            print(buttons)
            print(pos)

            if pos[0] < 0.1 and pos[0] > -0.1:
                clicks.append(pos)
                print(pos)
                continueRoutine = False
# # core.wait(0.1)
        win.flip()
win.close()

core.quit()