Importing Images from a File of Images

Hi all,

I’m very novice at PsychoPy and programming in general, so I’ve been completing several tutorials that I have found online and attempting some “easier” programming tasks before I get into the grind of programming my own experimental studies. Because I will be working with image stimuli in many of my planned experiments, as well as in several of the tutorials in which I am working, I was hoping to find an easy way to import several images, perhaps from a file in which I’ve stored all the images, together. I’ve been perusing through the old topics on this, and there have been several suggestions on how to randomize imported images and whatnot, but I can’t quite find the exact method to import images from a file in which I’ve stored the images.

I’ve worked with two options:

  1. I have the file of images saved to my Python files
  2. I have that file saved as above, and I also have a .csv Excel file with the paths of each image listed in row-format

I honestly am not positive where to go after that. I tried working with importing the images from their saved file using the os module, but I’m not positive that I’m doing that right. I tried also to import the path line from the .csv file, but I’m thinking I’m definitely doing that wrong. Unfortunately, my tutorials for which I am following just kind of threw this out as a task but offered little guidance on how to go about doing it. So below I show some examples of what I’ve been working with:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from psychopy import core, visual
import os, random, glob
win = visual.Window([800, 600], fullscr=False, monitor="testMonitor")
images = []
for file in os.listdir('image'):
    if file.lower().endswith(".jpg"):
        images.append(file)

I tried a glob approach, wasn’t sure entirely what I was doing to be honest (being that I am very novice at this).

This was the original way I tried to go about this (following from a tutorial I found online that besides this point has been very helpful in introducing me to PsychoPy):

from psychopy import visual, core, event, clock
import csv, random
import my

##Setup Section
win = visual.Window([800, 600], fullscr=False, monitor="testMonitor", units='cm')

# read stimuli file
trials = my.getStimulusInputFile('stimuli.csv')
numberColumn = 0    #number = trials[trialNumber][numberColumn]
filename = 1        #filename = trials[trialNumber][filename]
wordColumn = 2      #wordColumn = trials[trialNumber][wordColumn]
correctResponse = 3 #correctResponse = trials[trialNumber][correctResponse]

# turn the text string into stimuli
textStimuli = []
imageStimuli = []
for trial in trials:
    textStimuli.append(visual.TextStim(win, text=trials[wordColumn]))
    imageStimuli.append(visual.ImageStim(win, size=[0.5,0.5], image=trials[filename]))

*where my is a module that includes functions such as the one used in this line of code above.

Anyway, I’m just looking for a simple solution of how to import several images from a file of images. Maybe I’m close. Maybe I’m really far off. Any help you could provide would be greatly appreciated.

Thanks in advance!

Thanks jon. I’ve edited the original post to reflect that.

If I’ve followed correctly, I think your ‘original’ solution is close - try replacing trials[filename] with trial[filename] (i.e. removing the s from trials). This should give you a list (imageStimuli), where each item is an ImageStim that can draw the relevant image.

Thanks for the suggestion. I attempted to run the code by dropping “s” from trials, and I received the following error message:

12.9097 	ERROR 	Couldn't find image apple_implausible_eye_front.jpg; check path? (tried: C:\Users\name\AppData\Local\Programs\Python\Python36\apple_implausible_eye_front.jpg)
Traceback (most recent call last):
  File "C:\Users\name\AppData\Local\Programs\Python\Python36\Practice with Importing Images from Lists.py", line 23, in <module>
    imageStimuli.append(visual.ImageStim(win, size=[0.5,0.5], image=trial[filename]))
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\image.py", line 97, in __init__
    self.setImage(image, log=False)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\image.py", line 289, in setImage
    setAttribute(self, 'image', value, log)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\tools\attributetools.py", line 137, in setAttribute
    setattr(self, attrib, value)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\tools\attributetools.py", line 27, in __set__
    newValue = self.func(obj, value)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\image.py", line 276, in image
    forcePOW2=False)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\basevisual.py", line 794, in _createTexture
    raise IOError, msg % (tex, os.path.abspath(tex))
IOError: Couldn't find image apple_implausible_eye_front.jpg; check path? (tried: C:\Users\name\AppData\Local\Programs\Python\Python36\apple_implausible_eye_front.jpg)

I’m not sure why I’m getting this error message, because when I follow that path, I do find that particular image.

In retrospect, it looks like it’s a path issue afterall, i.e. PsychoPy is searching a slightly different path. Here’s where the image is actually stored:

C:\Users\name\AppData\Local\Programs\Python\Python36\**image**\apple_implausible_eye_front.jpg

And here’s where PsychoPy is looking

C:\Users\name\AppData\Local\Programs\Python\Python36\apple_implausible_eye_front.jpg

So now it’s a matter of correcting that path. Any tips on that?

Store your images close to your experiment.