Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a GIF in jupyter notebook using google colab?

I am using google colab and would like to embed a gif. Does anyone know how to do this? I am using the code below and it is not animating the gif in the notebook. I would like the notebook to be interactive so that one can see what the code animates without having to run it.

I found many ways to do so that did not work in Google colab. The code and the GIF of interest is below.

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

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

enter image description here

I tried some of the solutions provided.

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

and similarly

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

but got the error,

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

both times.

like image 611
user4933 Avatar asked Apr 08 '20 21:04

user4933


People also ask

How do I show a GIF in Google Colab?

You should see a screen with Google Drive File Stream wants to access your Google Account. After you allow permission, copy the given verification code and paste it in the box in Colab. In the notebook, click on the charcoal > on the top left of the notebook and click on Files.


Video Answer


2 Answers

For external gif, you can use Jupyter's display as @knoop's answer.

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

But for a local file, you need to read the bytes and display it.

!wget https://upload.wikimedia.org/wikipedia/commons/e/e3/Animhorse.gif
Image(open('Animhorse.gif','rb').read())
like image 100
korakot Avatar answered Oct 24 '22 02:10

korakot


I don't know why the above method doesn't work for me.

How to insert gif file from google drive to google colab

Step 1: Save gif in your google drive

Step 2: Ask google drive to open your file in google drive (This step is not directly related to original question.)

  • How to import data from google drive to google colab. Link

    1. In your Google Drive (“My Drive”), create a folder called data in the location of your choosing. This is where you will upload your data.

    2. From a Colab notebook, type the following:

from google.colab import drive         # tell colab to look at google drive
drive.mount('/content/drive')
  1. The commands will bring you to a Google Authentication step. You should see a screen with Google Drive File Stream wants to access your Google Account.

  2. After you allow permission, copy the given verification code and paste it in the box in Colab.

  3. In the notebook, click on the charcoal > on the top left of the notebook and click on Files.

  4. Locate the data folder you created earlier and find your data. Right-click on your data and select Copy Path. Store this copied path into a variable and you are ready to go.

  5. Dataset is now stored in a Pandas Dataframe

Step 3:

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

Step 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')
like image 1
Ying Avatar answered Oct 24 '22 01:10

Ying