3

I've tried to save a Matplotlib animation, but am getting an odd error. The snippet of (I believe) relevant code is:

  def showMovie(self):
    frameFnc = self.getNoisyImage
    initFnc =  self.initImage

    movie = animation.FuncAnimation(self.movieFig, frameFnc,
                                frames = len(self.noiseArrays),
                                init_func = initFnc,
                                interval=1, blit=True,
                                repeat = True)


    movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])
    print "Saved movie"
    plt.ion()
    plt.show()



if __name__ == '__main__':
    z = noiseTester()
    z.makeStaticNoiseImages()
    z.readPredList()
    z.showMovie()

The error I get is as follows:

Traceback (most recent call last):
  File "noiseTest6.py", line 134, in <module>
    z.showMovie()
  File "noiseTest6.py", line 123, in showMovie
    movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 718, in save
    writer.grab_frame(**savefig_kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 204, in grab_frame
    dpi=self.dpi, **savefig_kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 497, in print_raw
    renderer._renderer.write_rgba(filename_or_obj)
  RuntimeError: Error writing to file

When I check, filename_or_obj is an ', mode 'wb' at 0x7f3de99164b0>. This variable also has a name attribute, with a value of ''. The meaning of this string value, instead of the name I tried to assign to the file is not clear to me.

I know my animation is OK, because I've seen it work. It only has 500 frames, so I don't think it's too large. If anyone has any ideas about how I should chase down this Runtime error, I'd really appreciate it.

3
  • Is it possible that you forgot to post the actual error? What is the error message? Commented Jun 8, 2015 at 9:58
  • @hitzg - I supose that will teach me to post just before bed. I just added the actual Runtime error message. Commented Jun 8, 2015 at 11:55
  • 1
    I've been having the same issues a while back and couldn't figure it out too. I think it is related to libx264 and I believe that is has to do with the pixel size of the figure (I think x264 can't handle arbitrary sizes: height and width need to be multiples of 2, etc); I tried with adding a cropping filter to avconv/ffmpeg but that did not work... In the end I just rescaled the figure to various values and got it to work. Commented Jun 8, 2015 at 12:56

1 Answer 1

2

Well, using the info about libx264 from @hitzg , I just got rid of the arguments specifying its use - i.e.

movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])

was replaced by

movie.save("test.mp4", fps=10)

Now, things seem to be working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.