7

I've been able to create animated gifs that loop infinitely using PIL without an issue, usually ending up with something like

final_image.save('/path/to/images/some.gif, save_all=True, append_images=frames_images, duration=frame_speeds, loop=0)

I am now in a situation where I would like to create a gif that plays a single time and does not loop. The PIL docs I used¹ are pretty clear about the loop argument but don't offer any advice for my situation:

loop : int
The number of iterations. Default 0 (meaning loop indefinitely).

0 causes it to loop infinitely. 1 causes it loop once (play two times). I have tried options like -1 and None but can't find a working argument. I am currently using a work around where I invoke gifsicle afterwards to remove the loop entirely but was hoping PIL would support this natively

¹ - https://imageio.readthedocs.io/en/stable/format_gif-pil.html

user1026361
  • 3,627
  • 3
  • 22
  • 20
  • 1
    Surely loop=1 means it loops (and thus plays) once, not twice as you suggest? – Gavin Feb 11 '19 at 00:44
  • 1
    @Gavin Unfortunately no, loop=1 seems to result in the sequence playing two times – user1026361 Feb 11 '19 at 00:57
  • Might depend on the version of imageio / pillow - see the following merge request as it seems to be an incompatibility between older versions of imageio and _some_ gif players (ie chrome for instance). [imageio merge request](https://github.com/imageio/imageio/pull/237) – Gavin Feb 11 '19 at 01:05
  • @user1026361 it appears you have stumbled upon a bug in the current distribution of the Pillow for Python. I was able to replicate your issue, in fact the GIF will always loop `n+1` times when passed `loop=n` - I can only suggest submitting this as a bug with the maintainer and using your workaround to enforce a single loop for the time being. Note that @Gavin is correct, the issue is relevant - Chrome loops twice for `n=1`, while some viewers only loop once (and others loop the same .gif endlessly) – Grismar Feb 11 '19 at 01:11
  • Thank you @Grismar and Gavin - I've never encountered an issue with PIL and hadn't considered this might be a bug. I will keep my work around in play and follow up with the maintainer – user1026361 Feb 11 '19 at 04:19

2 Answers2

8

If you are using Pillow, you can leave out the loop argument. That'll ensure no loop. https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#saving Also to note, duration is in milliseconds, not seconds.

cbasavaraj
  • 105
  • 1
  • 5
0

What a funny coincidence. I have a code that shows a GIF one iteration and then freezes until the mp3 file is finished... but I need a GIF that loops infinitely.

The only difference is I used the library moviepy.


Working solution:

from moviepy.editor import *
import moviepy.editor as mp


# Import the audio(Insert to location of your audio instead of audioClip.mp3)
audio = AudioFileClip("PATH/TO/MP3_FILE")

clip = VideoFileClip("PATH/TO/GIF_FILE").set_duration(audio.duration)


# Set the audio of the clip
clip = clip.set_audio(audio)


clip_resized = clip.resize((1920, 1080)) 


# Export the clip
clip_resized.write_videofile("movie_resized.mp4", fps=24)
Maximilian Freitag
  • 939
  • 2
  • 10
  • 26