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.
libx264and 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.