ipython(jupyter)笔记本中的动画 - ValueError:关闭文件的I / O操作

时间:2017-02-09 10:01:48

标签: animation matplotlib ipython ipython-notebook jupyter-notebook

我有一个生成动画的jupyter笔记本。它正在我的旧笔记本电脑上工作(xubuntu gnu / linux)。现在在我的新笔记本电脑(trisquel gnu / linux)上它没有用。这让我觉得我有一个丢失的库或类似的东西,但我无法解决它。

作为测试,我回到教程,在那里我学习了如何首先进行jupyter笔记本动画(http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/)。当我运行代码单元[7]时,我遇到了同样的问题。

错误如下所示。

非常感谢任何帮助解决这个问题。

非常感谢:)

[编辑1]如果我尝试使用anim.save(...)代替HTML(anim.to_html5_video()),我会遇到同样的问题。

[编辑2]如果我将笔记本下载为* .py文件并使用常规python(而不是ipython)运行它,那么我会遇到同样的问题。

[编辑3]我的ffmpeg安装似乎正在工作,因为我能够从一系列图像手动创建动画。我现在可以使用它作为解决方法,但它非常笨重,HTML(anim.to_html5_video())解决方案应该工作,所以仍然需要进行排序。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-5114ccf53b4c> in <module>()
----> 1 HTML(anim.to_html5_video())

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in to_html5_video(self)
   1157                                 bitrate=rcParams['animation.bitrate'],
   1158                                 fps=1000. / self._interval)
-> 1159                 self.save(f.name, writer=writer)
   1160 
   1161             # Now open and base64 encode

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
   1011                         # TODO: See if turning off blit is really necessary
   1012                         anim._draw_next_frame(d, blit=False)
-> 1013                     writer.grab_frame(**savefig_kwargs)
   1014 
   1015         # Reconnect signal for first draw if necessary

/usr/local/lib/python2.7/contextlib.pyc in __exit__(self, type, value, traceback)
     33                 value = type()
     34             try:
---> 35                 self.gen.throw(type, value, traceback)
     36                 raise RuntimeError("generator didn't stop after throw()")
     37             except StopIteration, exc:

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in saving(self, *args, **kw)
    254             yield self
    255         finally:
--> 256             self.finish()
    257 
    258     def _run(self):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in finish(self)
    274     def finish(self):
    275         'Finish any processing for writing the movie.'
--> 276         self.cleanup()
    277 
    278     def grab_frame(self, **savefig_kwargs):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in cleanup(self)
    309     def cleanup(self):
    310         'Clean-up and collect the process used to write the movie file.'
--> 311         out, err = self._proc.communicate()
    312         self._frame_sink().close()
    313         verbose.report('MovieWriter -- '

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in communicate(self, input, timeout)
    925 
    926         try:
--> 927             stdout, stderr = self._communicate(input, endtime, timeout)
    928         finally:
    929             self._communication_started = True

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate(self, input, endtime, orig_timeout)
   1711             if _has_poll:
   1712                 stdout, stderr = self._communicate_with_poll(input, endtime,
-> 1713                                                              orig_timeout)
   1714             else:
   1715                 stdout, stderr = self._communicate_with_select(input, endtime,

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate_with_poll(self, input, endtime, orig_timeout)
   1767             select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
   1768             if self.stdout:
-> 1769                 register_and_append(self.stdout, select_POLLIN_POLLPRI)
   1770                 stdout = self._fd2output[self.stdout.fileno()]
   1771             if self.stderr:

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in register_and_append(file_obj, eventmask)
   1746             poller = select.poll()
   1747             def register_and_append(file_obj, eventmask):
-> 1748                 poller.register(file_obj.fileno(), eventmask)
   1749                 self._fd2file[file_obj.fileno()] = file_obj
   1750 

ValueError: I/O operation on closed file

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题(在ma​​c os 10.12.2上),并通过重新安装ffmpeg并启用x264编解码器来修复它,to_html5_video()函数需要将动画转换为html5

安装x264:

cd /my/path/where/i/keep/compiled/stuff
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static --enable-shared
make
make install
ldconfig # note: use for linux, not necessary for mac

source

要在启用x264编解码器的情况下安装ffmpeg:

cd /my/path/where/i/keep/compiled/stuff
git clone http://source.ffmpeg.org/git/ffmpeg.git
cd ffmpeg
./configure --enable-gpl --enable-libx264
make
make install
ldconfig # note: use for linux, not necessary for mac

source