1

I have seen so many different threads about this topic but none of their solutions seems to work for me. I've tried several ways of reading an image from my Drive into Colab using its URL, with no success. I want to read it using its URL rather than mounting my Drive and using directories because multiple people share this Colab, and their directory to the image might not be the same as mine.

The first attempt comes from a popular thread on this issue: How do I read image data from a URL in Python?

from PIL import Image
import requests

url = 'https://drive.google.com/file/d/1z33YPsoMe0lSNNa2XWa0tiK2571j2tFu/view?usp=sharing'

im = Image.open(requests.get(url).raw) # apparently no need for bytes wrapping in new Python versions
im = Image.open(requests.get(url, stream=True).raw) # also does not work

The error I got was UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f0189569770>

Then I tried:

from skimage import io

io.imshow(io.imread(url))

Which returned ValueError: Could not find a format to read the specified file in mode 'i'. Feeling very lost because all these approaches seem to work for everyone else. Would appreciate any feedback.

timpanists
  • 27
  • 1
  • 7

2 Answers2

3

Using gdown to read an image from Google Drive into Colab.

If you have your image in your Google Drive and you are using colab.research.google.com, then you can follow these steps:

  1. pip install gdown

  2. Obtaining the share link from your image (remember to set the option "Share to anyone with the link"). For an example: # https://drive.google.com/file/d/1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ/view?usp=sharing

  3. Extract the id of the share link from the URL 1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ.

  4. Download the image in the folder content of a particular user who has access to this Colab:

    !gdown --id '1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ' --output bird1.jpp

  5. Read the image file from the folder content

from PIL import Image
im = Image.open("../content/bird1.jpg") 
im
Fredy Rojas
  • 91
  • 1
  • 4
0

Read/Show Image with URL in Google Colab

from PIL import Image
import requests
url = 'https://s3.amazonaws.com/eit-planttoolbox-prod/media/images/Bellis_Perennis--Morgaine--CC_BY_2.0.jpg'
im = Image.open(requests.get(url, stream=True).raw) 
im