【问题标题】:How to display a GIF in jupyter notebook using google colab?如何使用 google colab 在 jupyter notebook 中显示 GIF?
【发布时间】:2020-07-21 10:12:47
【问题描述】:

我正在使用 google colab 并想嵌入一个 gif。有谁知道如何做到这一点?我正在使用下面的代码,它没有为笔记本中的 gif 设置动画。我希望笔记本是交互式的,这样人们就可以看到代码动画的内容,而无需运行它。

我发现了许多在 Google colab 中不起作用的方法。下面是代码和感兴趣的 GIF。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("/content/animationBrownianMotion2d.gif")
plt.imshow(img)

我尝试了提供的一些解决方案。

import IPython
from IPython.display import Image
Image(filename='/content/animationBrownianMotion2d.gif')

类似

import IPython
from IPython.display import Image
Image(filename='/content/animationBrownianMotion2d.gif',embed=True)

但得到了错误,

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-56bf6cd2b134> in <module>()
      1 import IPython
      2 from IPython.display import Image
----> 3 Image(filename='/content/animationBrownianMotion2d.gif',embed=True)
      4 

/usr/local/lib/python3.6/dist-packages/IPython/core/display.py in __init__(self, data, url, filename, format, embed, width, height, retina, unconfined, metadata)
   1013 
   1014         if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
-> 1015             raise ValueError("Cannot embed the '%s' image format" % (self.format))
   1016         self.width = width
   1017         self.height = height

ValueError: Cannot embed the 'gif' image format

两次。

【问题讨论】:

    标签: python matplotlib jupyter-notebook gif google-colaboratory


    【解决方案1】:

    对于外部 gif,您可以使用 Jupyter 的显示作为 @knoop 的答案。

    from IPython.display import Image
    Image(url='https://upload.wikimedia.org/wikipedia/commons/e/e3/Animhorse.gif')
    

    但是对于本地文件,需要读取字节并显示出来。

    !wget https://upload.wikimedia.org/wikipedia/commons/e/e3/Animhorse.gif
    Image(open('Animhorse.gif','rb').read())
    

    【讨论】:

    • 您可以使用 'filename' 作为关键字参数而不是 'url' 并且来自 IPython.display 的相同旧 Image 方法就像一个魅力:)
    • 我试过了,但没有。 ValueError: Cannot embed the 'gif' image format
    • 事实证明,你是对的。我认为我的代码有效,因为我添加了代码,但忘记执行它,并看到了之前执行的渲染图像。感谢您的提醒:)
    【解决方案2】:

    我不知道为什么上述方法对我不起作用。

    如何将 gif 文件从 google drive 插入到 google colab

    第 1 步:将 gif 保存到您的 Google 云端硬盘中

    第 2 步:让 google drive 在 google drive 中打开您的文件 (此步骤与原始问题没有直接关系。)

    • 如何将数据从 google drive 导入到 google colab。 Link

      1. 在您的 Google 云端硬盘(“我的云端硬盘”)中,在您选择的位置创建一个名为 data 的文件夹。这是您上传数据的地方。

      2. 在 Colab 笔记本中,键入以下内容:

    from google.colab import drive         # tell colab to look at google drive
    drive.mount('/content/drive')
    
    1. 这些命令将带您进入 Google 身份验证步骤。您应该会看到一个屏幕,其中包含 Google 虚拟云端硬盘想要访问您的 Google 帐户。

    2. 在您允许后,复制给定的验证码并粘贴到 Colab 的框中。

    3. 在笔记本中,点击笔记本左上角的木炭>,然后点击文件。

    4. 找到您之前创建的数据文件夹并找到您的数据。右键单击您的数据并选择复制路径。将此复制的路径存储到变量中,您就可以开始了。

    5. 数据集现在存储在 Pandas 数据帧中

    第 3 步:

    from IPython.core.interactiveshell import InteractiveShell
    InteractiveShell.ast_node_interactivity = "all"
    from IPython import display
    

    第 4 步:

    gifPath = Path("/content/drive/My Drive/xxxx/xxx.gif") # please paste the whole path from Step 2-6
    # Display GIF in Jupyter, CoLab, IPython
    with open(gifPath,'rb') as f:
        display.Image(data=f.read(), format='png')
    

    【讨论】:

      最近更新 更多