Randomly display multiple images together from a list of images

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2020.2.4
Standard Standalone? (y/n) If not then what?: yes, win 64bit
What are you trying to achieve?:
Hi everyone
I want the four images from a list to be randomly selected and shown to the subject in different pre-defined positions, in fact I want the location of the images in each trial to be different from the previous trial also change the image opacity of a particular position in each trial, for example the opacity of that image which locates at [0.5,0.25] position will change every trial.

What did you try to make it work?:
I created an Excel file and put the address of the photos in four column and a column containing opacity amounts then by writing the code I tried to instruct the program to randomly select the image in each trial.

# Begin Experiment code:
import random, xlrd
# randomize seed
#input file
# number of items to load
num_items = 4
#counters to hold next stimulus reference
cur_image1 = 0
cur_image2 = 0
cur_image3 = 0
cur_image4 = 0
cur_opacity = 0
# Begin Routine Code:
# open the excel file
inbook = xlrd.oepn_workbook(in_file)
insheet = inbook.sheet_by_index(0) # open specific sheet

#arrayes to hold out stimuli
image1 = []
image2 = []
image3 = []
image4 = []
opacity = []
# loop through the rows in the stimulus file
for rowx in range(1,num_items+1):
    #read in an entire row
    row = insheet.row_values(rowx)
    #save the images
# randomly shuffle the images
cur_image1 = cur_image1 + 1
cur_image2 = cur_image2 + 1
cur_image3 = cur_image3 + 1
cur_image4 = cur_image4 + 1
cur_opacity = cur_opacity + 1

What specifically went wrong when you tried that?:
When I run the test, neither the code is executed nor I receive an error from the program. The experiment ends at the same time as running the code and does not reach the image display step. When I replace the original code(codeInit_2) with the instruction, the program runs until the instruction are displayed, but when it reaches the code, it only shows the following message on the Experiment runner Stdout without any alert:

Running: C:\Users…\Image_task_lastrun.py ######

Thank you very much for your help.

Hi There,

Before going into a more complex code component solution. Please can I check, is there a reason you can’t use a conditions file (imported in your 'traceback; loop) with 4 columns for images , 4 columns for the positions of those images and 4 columns for the respective opacity of those images?

It sounds like you need quite a specific pseudorandomised order (i.e. no repeats at a single location) and since, at the moment, pseudoransomisation is not a supported options in the trials loop the easiest way to achieve that can be through creating your conditions file with trials in the pseudorandom order you wish to present them and selecting loopType as ‘sequential’.


Thank you very much for your response Becca,

I don’t use from the condition file like you said since as you know if I want to consider all the permutations of this four distinct images and display them at four corners of the screen I must build 4!=24 different rows at the condition file and it is boring.

Not necessarily boring … you could use python to make your conditions file for you :wink: This trick could be a useful one

import csv
import itertools

#a list of possible conditions (images)
myList=['a', 'b', 'c', 'd', 'e', 'f']

#get all permutations (of length 4)
allPerms = itertools.permutations(myList, r=4)

# Create a list of dictionaries to turn into a conditions file
for thisPerm in allPerms:
    toCSV.append({'Im1':thisPerm[0], 'Im2':thisPerm[1], 'Im3':thisPerm[2], 'Im4':thisPerm[3]})

#make a conditions file with keys as headers and values as rows
keys = toCSV[0].keys()
with open('conditions.csv', 'w', newline='')  as output_file:
    dict_writer = csv.DictWriter(output_file, keys)

This is probably quite similar to your initial approach to a solution actually?! (in making an excel file)

The only difference being that this would be run in advance of your experiment (outside of your builder programme). Of course, then there is the question of pseudo randomising (no repeats of an image in a location) and opacity - but it is possible a very similar trick could be applied?

Hope this helps,

Do you mean that I must creat a csv file by this code and then run it in my experiment?
Is there any code at the beginning of experiment/ beginning of routine and etc?
Can you please help me to do the same on an Excel file(my condition file at traceback loop)?

Yes, so the .csv file created from this code could be inserted to the ‘conditions’ field of your loop (it is a bit clearer once running it). Of course, this is purely an example, and it would need adapting for your image files and any other parameters you want to include in your conditions file.

You then wouldn’t need any code component to create the list at start of the experiment.

It shouldn’t matter if you feed in a .csv or a .xlsx file as both can be imported as conditions files in PsychoPy :slight_smile:


1 Like