如何使用 google colab 在 jupyter notebook 中显示 GIF?
How to display a GIF in jupyter notebook using google colab?
我正在使用 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
两次。
对于外部 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())
我不知道为什么上面的方法对我不起作用。
如何将 gif 文件从 google 驱动器插入到 google colab
第 1 步:将 gif 保存在 google 驱动器
步骤 2:让 google 驱动器打开 google 驱动器 中的文件
(此步骤与原题无直接关系。)
如何将数据从 google 驱动器导入到 google colab。 Link
在您的 Google 云端硬盘(“我的云端硬盘”)中,在您选择的位置创建一个名为 data 的文件夹。这是您上传数据的地方。
在 Colab 笔记本中,输入以下内容:
from google.colab import drive # tell colab to look at google drive
drive.mount('/content/drive')
这些命令会将您带到 Google 身份验证步骤。您应该会看到 Google 虚拟云端硬盘想要访问您的 Google 帐户的屏幕。
允许权限后,复制给定的验证码并粘贴到Colab中的框中。
在笔记本中,单击笔记本左上角的木炭 > 然后单击文件。
找到您之前创建的数据文件夹并找到您的数据。右键单击您的数据并 select 复制路径。将此复制的路径存储到变量中,您就可以开始了。
数据集现在存储在 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')
我正在使用 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
两次。
对于外部 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())
我不知道为什么上面的方法对我不起作用。
如何将 gif 文件从 google 驱动器插入到 google colab
第 1 步:将 gif 保存在 google 驱动器
步骤 2:让 google 驱动器打开 google 驱动器 中的文件 (此步骤与原题无直接关系。)
如何将数据从 google 驱动器导入到 google colab。 Link
在您的 Google 云端硬盘(“我的云端硬盘”)中,在您选择的位置创建一个名为 data 的文件夹。这是您上传数据的地方。
在 Colab 笔记本中,输入以下内容:
from google.colab import drive # tell colab to look at google drive
drive.mount('/content/drive')
这些命令会将您带到 Google 身份验证步骤。您应该会看到 Google 虚拟云端硬盘想要访问您的 Google 帐户的屏幕。
允许权限后,复制给定的验证码并粘贴到Colab中的框中。
在笔记本中,单击笔记本左上角的木炭 > 然后单击文件。
找到您之前创建的数据文件夹并找到您的数据。右键单击您的数据并 select 复制路径。将此复制的路径存储到变量中,您就可以开始了。
数据集现在存储在 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')