Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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")
from IPython.display import Image
Image(url='example.gif')
None of the above have worked so far.
Thanks
–
–
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:
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.
–
–
–
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
In some cases, you need to embed the gif file in Jupyter notebook, that is, the gif works in Jupyter notebook even if the gif file on your disk has been deleted. @user13013261's solution would fail, and @Const's only works in a Markdown cell.
You can try these:
display(Image(data=open(gif_path,'rb').read(), format='png'))
import base64
b64 = base64.b64encode(open(gif_path,'rb').read()).decode('ascii')
display(HTML(f'<img src="data:image/gif;base64,{b64}" />'))
Reference:
https://github.com/ipython/ipython/issues/10045#issuecomment-318202267
https://github.com/ipython/ipython/issues/10045#issuecomment-642640541
–
Haven't found a solution that embeds the updated GIF which is generated inside the notebook. So here is how:
Install tensorflow/docs pip install git+https://github.com/tensorflow/docs
, then
import tensorflow_docs.vis.embed as embed
embed.embed_file('output.gif')
The only solution that worked for me to display gif images on the notebook can be found here
For external gif, you can use Jupyter's display
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())
This answer from @korakot helped me a lot as I have tried every solution posted here but didn't work on my Kaggle notebook.
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)
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.