Samuele Bolotta
Samuele Bolotta

Reputation: 13

"FileNotFoundError" when importing Image from PIL on Google Colab

I want to import an image on Google Colab from PIL. By doing so:

from PIL import Image
img = Image.open('smallhouse.jpg')
img

However, I get this error.

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-20-8ab03a99a9db> in <module>()
      1 
      2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
      4 img

/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
   2807 
   2808     if filename:
-> 2809         fp = builtins.open(filename, "rb")
   2810         exclusive_fp = True
   2811 

FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'

Not sure why. Thanks!

Upvotes: 0

Views: 1146

Answers (1)

whoisraibolt
whoisraibolt

Reputation: 2698

1. You must upload the smallhouse.jpg image to your Google Drive.

2. In your .ipynb file, you need to include this code at the beginning of your application: drive.mount('/content/gdrive').

Go to the URL provided. Then, you will be prompted to give permission to access your Google Drive. Copy the code provided, access your application and paste the code there.

3. Finally, you can pass your image's path, which now is: /content/gdrive/My Drive/smallhouse.jpg.

Your code will look something like this:

# Imports
from google.colab import drive
from PIL import Image

# Mount Google Drive for fast, responsible access to files
drive.mount('/content/gdrive')

# Open image
img = Image.open('/content/gdrive/My Drive/smallhouse.jpg')
img

Upvotes: 2

Related Questions