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)
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):
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().
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.
Just one little point, not to be pedantic , 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
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 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.
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.