Link to images within google drive from a colab no

2020-03-24 05:41发布

问题:

I would like to store image files on a drive and link to them from a collaboration notebook. Is this possible? For example.

google-drive/
  notebook.ipynb
  images/
    pic.jpg

Within notebook.ipynb markdown cell:

![Alternate Text](images/pic.jpg)

回答1:

A possible alternative to the great answer by Arpit Gupta for images are publicly shared: Get the ID of your file and prepend this line:

https://docs.google.com/uc?export=download&id=

Grabbed it from this forum post.



回答2:

Use the following code:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

After running above command it will ask for the key. Click on the provided link and verify it.

Create a folder in drive using:

!mkdir -p colabData
!google-drive-ocamlfuse colabData

After this you can use the drive as if its connected locally:

%%bash
echo "Hello World...!!!" > colabData/hello.txt
ls colabData


回答3:

Google always loses me in the details. That's where the devil is. Based on the previous answer, these are my steps to make an image sharable so it can be used in a notebook.

  1. In your Google Drive, create a public folder as follows: a. Create a new folder and name it Image, for instance. b. Right-click the folder just created and select Share from the drop-down menu. c. In the popup dialog window, click the Advanced link in the bottom right. d. In the section Who has access, select "Public on the web - Anyone on the Internet can find and view". e. Click the Done button.
  2. Store the image in the folder you just created.
  3. Right-click on the image, and from the drop-down menu select Share.
  4. Click the Copy link button. A link is copied in your clipboard.
  5. From the link, copy the long image ID made of numbers and letters.
  6. Replace imageID in the following URL: https://docs.google.com/uc?export=download&id= with the ID you copied in the previous step.
  7. Copy this URL in the markdown image tag such as:![test](https://docs.google.com/uc?export=download&id=mmXXDD123zDGV51twxSCGAAX23)