Skip to content

Looping gifs with PillowWriter #11787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
t-makaro opened this issue Jul 28, 2018 · 3 comments · Fixed by #11789
Closed

Looping gifs with PillowWriter #11787

t-makaro opened this issue Jul 28, 2018 · 3 comments · Fixed by #11789

Comments

@t-makaro
Copy link
Contributor

t-makaro commented Jul 28, 2018

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.

@dopplershift
Copy link
Contributor

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

@t-makaro
Copy link
Contributor Author

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.

@jklymak
Copy link
Member

jklymak commented Jul 28, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants