How do I embed a gif in Jupyter notebook?

放肆的年华 提交于 2020-05-11 05:09:06

问题


I've been trying to display a gif in Jupyter notebook and have had some trouble. I keep getting a blank image file.

I've tried using html from this GitHub repository.

![wignerfunction][1](../gifs/wigner_rotation_animate.gif "wigner")

And

from IPython.display import Image
Image(url='example.gif')  

None of the above have worked so far.

Thanks


回答1:


I've been trying to display a gif in Jupyter notebook and have had some trouble.

To display gif in notebook you can use inline markup on a Markdown cell like this:

  • Relative reference in the same folder as ipynb notebook file:

    ![SegmentLocal](191px-Seven_segment_display-animated.gif "segment")
    
  • Relative reference in subfolder images from folder where ipynb notebook file is:

    ![SegmentLocal](images/191px-Seven_segment_display-animated.gif "segment")
    
  • Absolute reference from root folder irrespective of location of notebook file:

    ![SegmentLocal](/Users/username/some-folder/191px-Seven_segment_display-animated.gif "segment")
    
  • URL reference (should work out of the box in your notebook):

    ![ChessUrl](https://upload.wikimedia.org/wikipedia/commons/7/71/ChessPawnSpecialMoves.gif "chess")
    

As an example, since stack overflow is also using markdown, last line with url reference if given exactly last mentioned line: ![ChessUrl](https://upload.wikimedia.org/wikipedia/commons/7/71/ChessPawnSpecialMoves.gif "chess"), but not given as code reference evaluates to:

ChessUrl

As should be displayed in your jupyter notebook as well. Now, if you can see this last one ok, but can't see it from referenced local file you are most probably either having corrupted gif, permission issues or not proper file path.




回答2:


When I need to include a gif animation in a jupyter notebook I do the following:

<img src="FileName.gif" width="750" align="center">

The width and align parameters are defined by you.

Thanks




回答3:


Any image format either png, jpg, jpeg or gif etc, you want to show in python jupyter notebook then simply use matplotlib library.

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

img = mpimg.imread("/home/gaurav/assignment/sofcomputing/river.gif")

plt.imshow(img)


来源:https://stackoverflow.com/questions/51527868/how-do-i-embed-a-gif-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!