Gif does not play with MovieStim

I’m new to coding, and am trying to set up an experiment where I will show both show images and movies (gifs) at the same time. To begin with, I’m trying to keep it very simple, and just want to make sure that the stimuli are shown on the screen in the positions where I want them. I have no problems showing the images, and it looks like the movie is loaded as well, but it doesn’t play. I have looked at how it has been set up in the MovieStim coder demo, but it still doesn’t work in my script. However, when I load the movie/gif into the demo, it does work. Does anybody know what I am doing wrong?

Here is my code:

from psychopy import core, visual, event, data, gui,

#Create window
win = visual.Window(size=(1920, 1080), screen=1, pos=(0,0), monitor=‘testMonitor’, fullscr=False, allowGUI=True, color=(0,0,0), units=‘pix’)

#Responses
resp_key =

‘’‘’
Load images and videos
‘’’
image0 = “Stimuli/000.png”
image1 = “Stimuli/000a.png”
image2 = “Stimuli/000b.png”
image3 = “Stimuli/000c.png”

videoimage0 = “Stimuli/053.gif”
videoimage1 = “Stimuli/055a.png”
videoimage2 = “Stimuli/055b.png”
videoimage3 = “Stimuli/055c.png”

‘’’
Define the positions of each stimulus
Position 0 = Target
Position 1 = Left stimulus
Position 2 = Middle stimulus
Position 3 = Right stimulus
‘’’
positions = {0: (0, 150),
1: (-300, -150),
2: (-0, -150),
3: (300, -150)}

stim_size = (180, 180) #Stimulus size in pixels

#Create stimuli
#Condition 1 stimuli
imagetarget = visual.ImageStim(win, image0, units=“pix”, pos=positions[0], size=stim_size)
imageposition1 = visual.ImageStim(win, image1, units=“pix”, pos=positions[1], size=stim_size)
imageposition2 = visual.ImageStim(win, image2, units=“pix”, pos=positions[2], size=stim_size)
imageposition3 = visual.ImageStim(win, image3, units=“pix”, pos=positions[3], size=stim_size)

#Condition 2 stimuli
videoimagetarget = visual.MovieStim(win, “Stimuli/055.gif”, units=“pix”, pos=positions[0], size=stim_size, loop=True, autoStart=True)
videoimageposition1 = visual.ImageStim(win, videoimage1, units=“pix”, pos=positions[1], size=stim_size)
videoimageposition2 = visual.ImageStim(win, videoimage2, units=“pix”, pos=positions[2], size=stim_size)
videoimageposition3 = visual.ImageStim(win, videoimage3, units=“pix”, pos=positions[3], size=stim_size)

#Condition 1
#Draw the stimuli
while True:
imagetarget.draw()
imageposition1.draw()
imageposition2.draw()
imageposition3.draw()

#Show stimuli and wait for response
win.flip()

key = event.waitKeys(maxWait=600, keyList=['esc', '1', '2', '3',], clearEvents=True)
if 'esc' in key:
    break
elif '1' in key:
    resp_key.append(1)
    print("1 was pressed")
    break
elif '2' in key:
    resp_key.append(2)
    print("2 was pressed")
    break
elif '3' in key:
    resp_key.append(3)
    print("3 was pressed")
    break

#Condition 2
#Draw the stimuli
while not videoimagetarget.isFinished:
videoimagetarget.draw()
videoimageposition1.draw()
videoimageposition2.draw()
videoimageposition3.draw()

#Show stimuli and wait for response
win.flip()

key = event.waitKeys(maxWait=600, keyList=['esc', '1', '2', '3',], clearEvents=True)
if 'esc' in key:
    break
elif '1' in key:
    resp_key.append(1)
    print("1 was pressed")
    break
elif '2' in key:
    resp_key.append(2)
    print("2 was pressed")
    break
elif '3' in key:
    resp_key.append(3)
    print("3 was pressed")
    break

videoimagetarget.unload()

print(resp_key)

core.quit

I don’t see anything obviously wrong but it’s hard to tell because the code formatting isn’t appearing correctly on here. You want to enclose code in ` marks (not quotes, but the symbol left of the “1” key on a US keyboard) so the indentation is preserved.

Assuming videoimageposition1, 2, and 3 all appear correctly, then the isFinished loop isn’t the problem. If they aren’t appearing correctly, then it might something about the interaction between isFinished and loop. Otherwise, to troubleshoot further you could try calling videoimagetarget.play() directly instead of using autoStart.

Thank you for your reply. videoimageposition 1, 2 and 3 appear correctly, and the indentation is right (will make sure to use the ` marks another time). I have also tried to use videoimagetarget.play() after I flip the window. I asked ChatGPT for help, it suggested that I used videostim3 instead. This works both with a “while True loop” and isFinished. Now my problem is that when I try to set up three different loops that play one after the other (1st loop only shows images, 2nd loop shows a video and three images and the 3rd loop shows four gifs), the gifs appear on the screen, but they don’t play. Using .play() and autostart doesn’t seem to work. The problem is the same when I try with a .mov file.

As a general rule ChatGPT is not helpful with PsychoPy because its knowledge-base is a couple years old and some things (particularly the MovieStim libraries) have changed in that time. MovieStim3 is an older implementation that uses a different back-end system and has slightly more predictable behavior around certain things like the “isFinished” status.

The catch is that MovieStim3 is bad at restarting playback after hitting the end of a movie file. My recommendation would be to actually load three separate MovieStim3 objects, one for each loop.

I have had mixed results with animated gifs, sometimes because (I think) software sees ‘.gif’ and assumes a still image. Generally I convert them to .webm, m4a, mkv, or some other movie format. The converted files are often much smaller.

e.g., if you look at File:Tractography animated lateral view.gif - Wikimedia Commons

and run

ffmpeg -i 435px-Tractography_animated_lateral_view.gif 435.webm

command all on one line

(assuming the quality is ok for you), this might help. I realize this is not answering your question, but it should work.

Animated gifs typically have large files and are resource hungry to play.

Thank you for your suggestion. I tried to set one trial with the four movies up in builder, and generated a script. I can now see that it also makes separate loops using the MovieStim.