How to get images to display

Hi guys,

I’m programming an experiment that is meant to display images. I can get images to open in other python environments, but not in PsychoPy.

import os, random
from random import randint

#window
myWin = visual.Window([600,400],fullscr=True,allowGUI=True, monitor='laptop', units='deg')
myMouse = event.Mouse(visible=False, newPos=None, win=None)

living_images = []
for file in os.listdir('living_images') # <- I have tried this with the direct path. 'living_images' is in the working directory
    if file.lower().endswith(".jpg"):
        living_images.append(file)
 
random.shuffle(living_images)

non_living_images = []
for file in os.listdir('non_living_images'):
    if file.lower().endswith(".jpg"):
        non_living_images.append(file)
random.shuffle(non_living_images)

imageIndex = 0 
for i in range(10):
    cat_choice = randint(0,1)
    if cat_choice == 0:
        stimulus = non_living_images[imageIndex]
    else: 
        stimulus = living_images[imageIndex]
    imageIndex += 1
    visual.ImageStim(myWin, image=stimulus)
    stimulus.draw([myWin])
    myWin.flip()
    core.wait(5)

I get the following error:

OSError: Couldn't find image 'moose.jpg'; check path? (tried: D:\PhD\Paradigms\Implicit memory\moose.jpg)

Which is meant to be looking in:
D:\PhD\Paradigms\Implicit memory\Living_images for the moose.jpg

Could anyone please explain why this is not working.

Any help is greatly appreciated!

Hi,
there are a few problems in your script:

  • you forgot to import psychopy and the classes you need (visual, event, core) to use.

  • your images cannot be found because you are not providing the directory. When you pass the name of the stimulus to visual.ImageStim(myWin, image=stimulus) you give only the name of the stimulus, but there is no information about the directory. In if statement you could add add something like the following (It’s an ugly solution but it should work):

      if cat_choice == 0:
             stimulus = "non_living_images/" + non_living_images[imageIndex] 
      else: 
             stimulus = "living_images/" + living_images[imageIndex]
    
  • you must instantiate/declare visual.ImageStim(myWin, image=stimulus) .
    Just write something like stim = visual.ImageStim(myWin, image=stimulus) and in the following line use stim.draw() instead of stimulus.draw().

I hope this helps.

Cheers
Emanuele

1 Like

I would like to add to @porcu.emanuele’s comment.

This obviously won’t work. You need to import random to use random.shuffle().

I think a better solution would be using glob and a list comprehension here:

import glob
import os
import random

living_images = [os.path.join('living_images', image) for image in 
                 glob.glob('living_images/*.jpg')]
random.shuffle(living_images)

May I ask which editor you’re using? Any halfway decent programming environment would have discovered the missing imports immediately.

1 Like

Just one little point, not to be pedantic :slight_smile:, but he’s actually importing random together with os. But in any case he does not need to import it again to use randint.
Cheers
Emanuele

1 Like

Oh, you’re right I guess that’s why I only use one import per line! Tend to overlook stuff otherwise :wink:

Thank you for your replies guys! I implemented Emanuele’s suggestion last night and it worked fine. And yeah I forgot I had already imported random :blush: Thank you for your suggestion richard, I will have a go at using the glob and a list comprehension - I haven’t used these before, but I’m always looking to make my coding more efficient.

1 Like

And not a bad idea to import random functions from numpy rather than Python’s standard library, e.g. this is the standard Builder import line:

from numpy.random import random, randint, normal, shuffle

They are largely similar libraries but numpy.random has a few more bells and whistles (e.g. distributions to sample from). I guess you don’t want to change random libraries mid-course if setting seeds and so on is important, so probably good to start with the more comprehensive option.

2 Likes

Thanks for your suggestion Michael. Very much appreciated.