Randomize Image and Video Stimuli

OS: Windows 11
PsychoPy version: v2024.2.4
Standard Standalone?: Yes

What are you trying to achieve?:
Trying to make an experiment that has image and video stimuli. I am trying to make it randomized and display either one of the video stimuli, or one of the images.

What did you try to make it work?:
I tried adding in some custom code (in the builder) to make it so if the file type (from the excel sheet: a column that indicates if its “Static” or “Motion”) was an image, it would display that file as an image with the image stimuli property or else if it was a movie file type (“motion”), it would display that file with the movie stimuli property. Instead, I tried making start conditions (see below).

What specifically went wrong when you tried that?:
It still tries to pull the file into both the image/movie properties which causes it to crash. I have played them alone (versions of the experiment that only has videos or only movies) , but it seems like the movie just crashes most often when its together.

I made it as the image and video start conditions to check the file type before it starts but it still crashes either way. Here is an example for the Image property (the video one has basically the same thing but its looking for .mp4)

both draw from the $Stimulus_File column in my csv file, which has file locations for the stimuli.

No matter what I do, the video keeps trying to load as an image.
image

I apologize for the informality. I have tried many different ways to approach this but I do not understand how to get the image property to not load an MP4. Just a smidgen frustrated since I feel like this should be simple, but I cannot figure it out.

I have never tried .endswith

I would recommend that you add a new column for the stimulus type so you can have a simple condition.

I did try to add custom code that would check the stimulus file type (one column, “File_Type”, that specifically refers to the file as “Motion” or “Static”). I added a portion before the routine starts to check if File_Type was “Motion” or “Static” and then display it accordingly (using setAutoDraw(True) or making sure it stays off with setAutoDraw(False)). It still tries to load the video into the image property.

I tried something else and made the condition of the Image/Video only show if the file type was “Static” or “Motion” (respectively). The video still tries to load as an image. Images seem to work fine.

If your image component is trying to show your movie stimuli then please show your current code and component.

You shouldn’t need any code.

Will get back to you later tonight (~6.5 hours). Thank you though :pray::pray:

Actually, here. I am not sure what part you needed from me specifically, but I can send my Python code and some screenshots of the components (which I assume are the properties?).


Pilot_Stereogram_Experiment_FINAL_VideoandImage_lastrun.py (44.9 KB)

You shouldn’t need the $.

However, I think that the main issue is that the image component tries to load the movie, even if it isn’t going to play it.

The solution is to have two custom variables set in code.

if File_Type == "Motion":
     thisMovie = stimFile 
     thisImage= "default.png"
else:
     thisMovie = "default.mp4"
     thisImage = stimFile

Hi again!

I tried making the code and implementing what you said (also removing the $ from the condition). It doesnt seem like the movie crashes the program anymore! But the program crashes before it even goes into a trial. I am not really sure what crashes the experiment.

Generating PsychoPy script...

## Running: C:\Users\V\ASU Dropbox\Madelaine Vu\THESIS\Programs\Pilot_Stereogram_Experiment_FINAL\Pilot_Stereogram_Experiment_FINAL_VideoandImage_lastrun.py ##
28462.2961     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
28462.3282     INFO     Loaded monitor calibration from ['2025_01_03 18:21']
28462.3548     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
28462.3620     INFO     Loaded monitor calibration from ['2025_01_03 18:21']
28462.4066     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\iohub\start_iohub_process.py", line 56, in run
    s.udpService.start()
  File "C:\Program Files\PsychoPy\lib\site-packages\gevent\baseserver.py", line 336, in start
    self.init_socket()
  File "C:\Program Files\PsychoPy\lib\site-packages\gevent\server.py", line 229, in init_socket
    self.socket = self.get_listener(self.address, self.family)
  File "C:\Program Files\PsychoPy\lib\site-packages\gevent\server.py", line 239, in get_listener
    return _udp_socket(address, reuse_addr=cls.reuse_addr, family=family)
  File "C:\Program Files\PsychoPy\lib\site-packages\gevent\server.py", line 284, in _udp_socket
    sock.bind(address)
  File "C:\Program Files\PsychoPy\lib\site-packages\gevent\_socketcommon.py", line 543, in bind
    return self._sock.bind(address)
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted: ('', 9036)
############ Experiment ended with exit code 3489660927 [pid:10920] ############

Here is the Runner, though I am not sure if there is anything useful with it. I think the code works! But it wont show anything now? Again, it crashes before anything shows up. It just shows a black screen with no buttons and doesnt seem to save anything either since it crashes.

See here

Best wishes Jens

Did that! The program runs now, but it still tries to load the .mp4 as an image.

## Running: C:\Users\V\ASU Dropbox\Madelaine Vu\THESIS\Programs\Pilot_Stereogram_Experiment_FINAL\Pilot_Stereogram_Experiment_FINAL_VideoandImage_lastrun.py ##
303.7199     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
303.7274     INFO     Loaded monitor calibration from ['2025_01_03 18:21']
303.7553     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
303.7626     INFO     Loaded monitor calibration from ['2025_01_03 18:21']
303.8360     EXP     Imported ImageStimuli.csv as conditions, 10 conditions, 3 params
0.1659     ERROR     Found file 'resources/Motion_Diverge_Bunny.mp4', failed to load as an image
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\basevisual.py", line 1066, in _createTexture
    im = Image.open(filename)
  File "C:\Program Files\PsychoPy\lib\site-packages\PIL\Image.py", line 3536, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file 'C:\\Users\\V\\ASU Dropbox\\Madelaine Vu\\THESIS\\Programs\\Pilot_Stereogram_Experiment_FINAL\\resources\\Motion_Diverge_Bunny.mp4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\V\ASU Dropbox\Madelaine Vu\THESIS\Programs\Pilot_Stereogram_Experiment_FINAL\Pilot_Stereogram_Experiment_FINAL_VideoandImage_lastrun.py", line 1045, in <module>
    run(
  File "C:\Users\V\ASU Dropbox\Madelaine Vu\THESIS\Programs\Pilot_Stereogram_Experiment_FINAL\Pilot_Stereogram_Experiment_FINAL_VideoandImage_lastrun.py", line 541, in run
    StimImage.setImage(Stimulus_File)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\image.py", line 374, in setImage
    setAttribute(self, 'image', value, log)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py", line 140, in setAttribute
    setattr(self, attrib, value)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py", line 29, in __set__
    newValue = self.func(obj, value)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\image.py", line 349, in image
    self.isLumImage = self._createTexture(
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\basevisual.py", line 1073, in _createTexture
    raise IOError(msg % (tex, os.path.abspath(tex)))
OSError: Found file 'resources/Motion_Diverge_Bunny.mp4' [= C:\Users\V\ASU Dropbox\Madelaine Vu\THESIS\Programs\Pilot_Stereogram_Experiment_FINAL\resources\Motion_Diverge_Bunny.mp4], failed to load as an image
0.2588     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
################ Experiment ended with exit code 1 [pid:22688] #################

I have no idea how to stop movies from loading as an image. The code (sent by WakeCarter) has the if else to check the File_Type and the conditions of the components should also try and hinder it as well, but it still manages to load the video as an image.


Your components should now have $thisImage and $thisMovie.

ABSOLUTE LIFESAVER THANK YOU!! Yes, that worked perfectly! I just have to adjust some aspect ratio things, but it worked very well. I wish I knew this program better to understand that easy fix. Thank you so much.

1 Like