如何在Google Colab中读取本地图片?解决cv2.imread路径报错问题
Ah, I’ve been there—trying to pull a local image into Colab using cv2.imread() and hitting that frustrating "file not found" error. The key thing to remember here is: Google Colab runs on cloud servers, not your local machine. So paths like C:\Users\Admin\Desktop\tumor don’t exist in Colab’s environment. Let’s go through the three most reliable ways to get your image into Colab and read it with OpenCV.
1. Upload Files Directly to Your Colab Session
This is the quickest method for one-off uploads of small files. Colab provides a built-in tool to upload files from your local machine to the current session’s temporary storage:
from google.colab import files import cv2 import matplotlib.pyplot as plt # Trigger the file upload dialog uploaded = files.upload() # Grab the filename of your uploaded image (assuming you uploaded one file) image_filename = next(iter(uploaded.keys())) # Read the image with OpenCV img = cv2.imread(image_filename) # Quick check to confirm it worked (note: cv2 reads in BGR format, so we convert to RGB for display) if img is not None: plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() else: print(f"Failed to read {image_filename}—double-check the file extension!")
⚠️ Heads up: Files uploaded this way are temporary—they’ll disappear when your Colab session ends. You’ll need to re-upload if you restart the notebook.
2. Mount Your Google Drive (For Persistent Access)
If you want to keep your images accessible across sessions, mount your Google Drive. This lets Colab access files stored in your Drive directly:
from google.colab import drive import cv2 import matplotlib.pyplot as plt # Mount your Drive (follow the prompt to authenticate with your Google account) drive.mount('/content/drive') # Define the path to your image in Drive (adjust this to match your actual folder structure) # Example: If your image is in "My Drive/Medical Images/tumor.png" image_path = '/content/drive/MyDrive/Medical Images/tumor.png' # Read the image img = cv2.imread(image_path) # Verify and display if img is not None: plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() else: print(f"File not found at {image_path}—check the path and file extension!")
Pro tip: Use the left sidebar’s file explorer (📁 icon) to navigate your mounted Drive and copy the exact file path—this avoids typos!
3. Use the Colab File Explorer GUI (No Code Needed for Upload)
If you prefer a visual approach:
- Click the folder icon in the left sidebar to open the file explorer.
- Click the "Upload" button (↑) at the top of the explorer.
- Select your local image file from the pop-up dialog.
- Once uploaded, right-click the file in the explorer and select "Copy path".
- Paste that path directly into
cv2.imread():import cv2 img = cv2.imread('/content/tumor.jpg') # Replace with your copied path
Quick Troubleshooting Tips
- Always include the full file extension (
.jpg,.png, etc.)—cv2.imread()won’t guess it for you. - Colab uses forward slashes (
/) in paths, not Windows-style backslashes (\). - If
imgreturnsNone, double-check the path spelling and make sure the file was uploaded correctly.
内容的提问来源于stack exchange,提问作者Mrinalini madhusudan




