Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an animated gif with PIL that does not loop

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

like image 570
user1026361 Avatar asked Feb 11 '19 00:02

user1026361


1 Answers

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.

like image 141
cbasavaraj Avatar answered Oct 02 '22 22:10

cbasavaraj