Reputation: 3657
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
Upvotes: 8
Views: 7194
Reputation: 1049
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)
Upvotes: 0
Reputation: 125
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.
Upvotes: 9