Presenting image stimuli

Every now and then you will need to create experiments in PsychoPy3 that show images to the participants. For example an experiment where participants have to evaluate emotion on images. This section will focus on teaching you how to insert images within your experiment screen, and how to manipulate their size and location on the screen.

To start enabling Python to work with an image, we must first import the right modules. For now we will import the visual module and the event module from psychopy. However the images we will be using are saved somewhere in our computer, so we will also need a module for retrieving files from a directory. The glob module has this function. Note that we are not importing the glob module from psychopy, we are directly importing it from python. See below how it should be imported.

from psychopy import visual, event
import glob
Setting image path

First, it is important to tell psychopy where to look for our image files. To keep things tidy, the recommendation is to create a subdirectory in the same directory where your psychopy script is saved. You could also have you image files I the same directory as your psychopy script, but if you have many files that can become a mess.

Once you have made a dedicated directory for your stimulus images, you can create a variable called path and give it the name of the subdirectory, that is, the path relative to your script file . Notice that the path to your image is put in between quotation marks.
path = "images/"

Note: Sometimes the paths don't work and you have to play around with the brackets. For some users the double slash "//" works, for others the "\" single backslash works. This sometimes depends on whether you are using a Windows or a Mac operation system.

Creating the Window

To create a window containing your image we simply use the exact same steps for creating a window with textual stimuli, however instead of the TextStim() function, we use the ImageStim() function. Inside the ImageStim function's brackets, we select our window that we named win, and then we specify that the image we want to be used is equal to "nameofimage.jpg" or "nameofimage.png". The image I use is named horsie and it is of type jpg as you can see in the code below.

from psychopy import visual, event                       #Here we import the relevant modules.
import glob

path = "images/"  #Here we specify our image path.

win = visual.Window(color = 'pink')                      #Here we create our window named win.

stimulus = visual.ImageStim(win, image = path + "horsie.jpg")    #Here we create our image object. 

stimulus.draw()                                          #Here we draw our image.
win.flip()                                               #Here we flip the window.
event.waitKeys()                                         #Here we tell it to wait for a keypress
win.close()                                              #Here we close down the window
Figure 3.8: An example of how the image popped up in PsychoPy3. Notice I did not choose the FullScreen option in the code this time.

Functions and Image Stimuli

The same function we used for textual stimuli can be used with image stimuli, however there are some minor changes and additions.

We start with importing the modules and then creating our path like we did earlier for a single image. Note that we have imported the pandas module this time as well, which is a module for creating data-frames. After that is done, we create a variable images that we use to specify which images in our folder we would like to use. In the example below I use all the jpg type files in my folder. We use the glob.glob function to retrieve the images I ask it to. Inside the function I specify the variable we called path as the location the files a located in and I specify that I only want the jpg type files with the code "*.jpg".

from psychopy import visual, event
import glob, pandas

path = "images/"

images = glob.glob(path + "*.jpg")

print(images)

Note that we could have asked for only the images that contained the word horse in their filename by using this code instead "*horse.jpg".

After I have created the variable containing all the images that I need, I run print(images) to make sure that the right images have been retrieved.

Now we can create our experiment loop which will show the pictures one-by-one! Again we have a list, this time consisting of image names, that we want to loop through.

  • Create a stimulus object using the visual.ImageStim() function.
  • Then we would like to draw this stimulus.
  • Flip the window.
  • Wait for any key presses before closing the window.
  • And finally we would like to close the window.

This loop repeats itself for each image in the data-frame. Inside the visual.ImageStim() function we first specify which window we would like it to open, which is the win window in our case. Next we need to specify which image it need to choose each time. Remember that the image names gets loaded into the variable i in our loop. So in each iteration of the loop image = i. If we want to specify a different position for our images from the default position, we can add the code , pos = [0.5,0.5] into the function and play around with the numbers until we get the position we want. The first number stands for the vertical position of the image, whilst the latter number stands for the horizontal position of the image.

for i in images:                                                # loop through the list of image names 
    stimulus = visual.ImageStim(win, image = i, pos = [0,0])    # load the i'th image    
    stimulus.draw()                                             # draw the i'th image
    win.flip()                                                  # flip the window (make it visible)
    event.waitKeys()                                            # wait for any key press

win.close

Below is an overview of all the code used to create the image-loop.

#Here we import the necessary modules:
from psychopy import visual, event                                
import glob

#Here we specify the path to the folder containing our images:
path = "images/"           

#Here we create a list containing all the images we specify from the folder they're in:
images = glob.glob(path + "*.jpg")
print(images)

#Here we create our window object:
win = visual.Window(color = 'pink') 

#Here we create a loop that loops through each image in our list of image file names
#The loop then creates an image object for the first image, draws it on the window, 
#flips it and closes the window when the user presses any key on the keyboard.
#Then the loop moves on to the next image and repeats the process:
for i in images:                                                          
    stimulus = visual.ImageStim(win, image = i, pos = [0,0])  
    stimulus.draw()                                                               
    win.flip()                                                                    
    event.waitKeys()
win.close

Exercise 1: Two images in one screen

In this exercise I want you to play around with the positioning of the image, so that you might be able to insert two images in one window, so it looks something like this:

Exercise 2: Images and text together

In this exercise, try to insert text under the images from the exercise above. For example:

results matching ""

    No results matching ""