Skip to content

Looping gifs with PillowWriter #11787

Closed

Description

@t-makaro

Bug report

Bug summary

Saving a gif using PillowWriter produces a gif that does not loop. Is there a way to make the gif loop endlessly using PillowWriter?

Code for reproduction

from matplotlib.animation import FuncAnimation, PillowWriter
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 1, 20)

def f(x, i):
    return x*np.sin(i/10)

line, = plt.plot(x, f(x, 0))
plt.xlim([0,1])
plt.ylim([-1,1])
fig = plt.gcf()
def animate(i):
    line.set_data(x, f(x,i))

anim = FuncAnimation(fig, animate, frames=60, interval=50)
anim.save('fig.gif', writer=PillowWriter(fps=20))
plt.show()

Actual outcome

fig

Expected outcome

Same gif, but I want it to loop once it finishes.

Matplotlib version

  • Operating system: Windows 10 Version 1803
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6.5

Matplotlib was installed with conda.

Activity

  1. dopplershift commented on Jul 28, 2018

    @dopplershift
    Contributor

    If someone knows the incantation to use with pillow to make a gif loop, then we can add it.

  2. t-makaro commented on Jul 28, 2018

    @t-makaro
    ContributorAuthor

    I found it. According to the Pillow Docs, adding the loop parameter to the save call to sets the number of times the gif loops.

    I found this, and he set loop=0. One might think this that would be not looping (I guess 1 is not looping), but I decided to try it. I simply added loop=0 to the call on this line (I literally just modified that line in my site-packages), and lone behold endless looping.

    Presumably, we should make this configurable.

    self.loop = kwargs.pop("loop", 0)
    

    to the __init__ of PillowWriter, and then loop=self.loop to the call to save.

  3. jklymak commented on Jul 28, 2018

    @jklymak
    Member

    Seems like a reasonable addition to the API. If you'd like to try a PR folks here will help you...

  4. added this to the v3.1 milestone on Jul 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions