如何在Jupyter笔记本中嵌入gif?

iio*_*oii 6 python ipython jupyter

我一直试图在Jupyter笔记本中显示gif,但遇到了一些麻烦。我不断得到一个空白的图像文件。

我试过使用此GitHub存储库中的 html 。

![wignerfunction][1](../gifs/wigner_rotation_animate.gif "wigner")
Run Code Online (Sandbox Code Playgroud)

from IPython.display import Image
Image(url='example.gif')  
Run Code Online (Sandbox Code Playgroud)

到目前为止,以上方法均无效。

谢谢

Con*_*nst 11

我一直试图在Jupyter笔记本中显示gif,但遇到了一些麻烦。

要在笔记本中显示gif,可以在Markdown单元格上使用内联标记,如下所示:

例如,由于堆栈溢出也使用markdown,因此如果给出的URL的最后一行恰好在最后提到的那一行:![ChessUrl](https://upload.wikimedia.org/wikipedia/commons/7/71/ChessPawnSpecialMoves.gif "chess"),但没有作为代码引用给出,则得出:

国际象棋

也应在jupyter笔记本中显示。现在,如果您可以看到最后一个确定,但是无法从引用的本地文件中看到它,则您很可能是gif损坏,权限问题或文件路径不正确。

  • 您必须将单元格类型设置为 markdown。也许应该在主要评论中强调这一点 (2认同)

Ler*_*ang 6

在某些情况下,您需要将gif文件嵌入Jupyter笔记本中,也就是说,即使磁盘上的gif文件已被删除,该gif在Jupyter笔记本中仍然可以工作。@user13013261 的解决方案将失败,而 @Const 的解决方案仅适用于 Markdown 单元格。

您可以尝试这些:

display(Image(data=open(gif_path,'rb').read(), format='png'))
Run Code Online (Sandbox Code Playgroud)

或者

import base64
b64 = base64.b64encode(open(gif_path,'rb').read()).decode('ascii')
display(HTML(f'<img src="data:image/gif;base64,{b64}" />'))
Run Code Online (Sandbox Code Playgroud)

参考:
https://github.com/ipython/ipython/issues/10045#issuecomment-318202267 https://github.com/ipython/ipython/issues/10045#issuecomment-642640541


use*_*261 5

当我需要在 jupyter notebook 中包含 gif 动画时,我会执行以下操作:

<img src="FileName.gif" width="750" align="center">
Run Code Online (Sandbox Code Playgroud)

宽度和对齐参数由您定义。

谢谢