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:
- I have the file of images saved to my Python files
- 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!