psychopy.org | Reference | Downloads | Github

How to unpack image number or colour number from a file name

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac Big Sur
PsychoPy version (e.g. 1.84.x): 2020.2.10
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:

I would like to select a new image based on the image number or image colour of selected_image_5 and selected_image_1. For example, ‘{image_number_selected_image_5}_{image_colour_selected_image_1}.jpg’ . selected_image_5 and selected_image_1 are selected from the below coding. May I know how can I find out the image number or image colour of selected_image_5 and selected_image_1? Thanks a lot!

# Begin experiment:

Import random

# Begin routine:

# select different colours for different images

colour_list= ['blue', 'brown', 'yellow', 'green', 'red', 'grey', 'orange'] # create the colour list
colour  = random.sample(colour_list, 5) # randomly select 5 colours from the list

# assign different colours to different images
image1_colour = colour_list[1] 
image2_colour = colour_list[2]
image3_colour = colour_list[3]
image4_colour = colour_list[4]
image5_colour = colour_list[5]

# image 1
image1_file = f'VCS_{image_number}_in_{image1_colour}.jpg'

# image 2
image2_number = image_number + 72
if image2_number > 360:
    image2_number = image2_number -360
    
image2_file = f'VCS_{image2_number}_in_{image2_colour}.jpg'


# image 3
image3_number = image_number + 144
if image3_number > 360:
    image3_number = image3_number -360
    
image3_file = f'VCS_{image3_number}_in_{image3_colour}.jpg'

# image 4
image4_number = image_number + 216
if image4_number > 360:
    image4_number = image4_number -360
    
image4_file = f'VCS_{image4_number}_in_{image4_colour}.jpg'

# image 5
image5_number = image_number + 288
if image5_number > 360:
    image5_number = image5_number -360
    
image5_file = f'VCS_{image5_number}_in_{image5_colour}.jpg'

# randomly select four to show 
selected_image_list=[image1_file, image2_file, image3_file, image4_file, image5_file]
selected = random.shuffle(selected_image_list)

selected_image_file1 = selected_image_list[1] 
selected_image_file2 = selected_image_list[2]
selected_image_file3 = selected_image_list[3]
selected_image_file4 = selected_image_list[4]
selected_image_file5 = selected_image_list[0]