Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

html - How can I open images in a Google Colaboratory notebook cell from uploaded png files?

I am working with a Google Colaboratory notebook. I uploaded a file named bp.png into the working directory, and I can see that the file is in there by running !ls in a code cell. Then I try this piece of code to see the image in a markdown cell:

<h2 align="center">Image</h2>
<img src="bp.png" width="600">

But the Colab notebook's cell stays empty after running that (except for the header), although if I run this in a local Jupyter notebook the image does appear in the cell in that local notebook.

UPDATE:

I know I can use files uploaded to the working directory because my custom .py files that I upload, get imported to my Colab notebooks without any problems. For example, I can upload a file py_file.py and then in the Colab notebook use it as in from py_file import some_function, and it works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

from IPython.display import Image
Image('bp.png')

You can set width and height as well

Image("bp.png", width=100, height=100)

To display more than 1 image, you need to call display. (it’s auto for just 1 image)

from IPython.display import Image, display
display(Image('1.png'))
display(Image('2.png'))

Update jan/2019

Put your image in /usr/local/share/jupyter/nbextensions/

Then display it from /nbextensions/, e.g.

%%html
<img src='/nbextensions/image.png' />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...