You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何向Google Colab导入图片?新手实操报错求正确流程

How to Load Local Images into Google Colab (No More Errors!)

Hey there! I’ve walked many developers through fixing image upload issues in Colab, so let’s break down the exact, reliable workflows to get your local images loaded and ready for processing.

Method 1: Manual Upload (Great for Small Numbers of Images)

This is the simplest way if you only have a few images to work with:

  • Open your Colab notebook, then click the Files icon (folder shape) in the left sidebar.
  • Hit the Upload button at the top of the file panel, then select the image files from your local laptop.
  • Once upload finishes, you’ll see the files in the list. You can directly reference them by filename in your code, like:
    import matplotlib.pyplot as plt
    img = plt.imread("vacation_photo.jpg")
    plt.imshow(img)
    
  • Heads up: These files will disappear when your Colab session ends. If you need to keep them long-term, move them to Google Drive or download them back to your laptop.

Method 2: Code-Based Batch Upload (Perfect for Multiple Images)

If you’ve got a bunch of images, manual upload gets tedious—use this code approach instead:

  1. Run this snippet to trigger a local file selection window:
    from google.colab import files
    uploaded = files.upload()
    
  2. Choose all the images you want to upload from your laptop. The console will show progress for each file.
  3. Verify the files are in your current working directory with:
    import os
    print(os.listdir())
    
  4. Load and process the images using libraries like PIL or matplotlib. Here’s a quick example:
    from PIL import Image
    import io
    
    # Loop through all uploaded images
    for filename in uploaded.keys():
        # Load the image from the uploaded bytes
        img = Image.open(io.BytesIO(uploaded[filename]))
        # Add your processing logic here (resize, filter, etc.)
        print(f"Loaded image: {filename}, size: {img.size}")
        img.show()
    

Method 3: Mount Google Drive (For Large Files or Long-Term Storage)

If your images are huge or you want to keep them accessible across sessions, upload them to Google Drive first, then mount Drive in Colab:

  1. Run this code to authenticate and mount your Drive:
    from google.colab import drive
    drive.mount('/content/drive')
    
  2. Follow the prompt to log into your Google account, copy the authorization code, and paste it into the Colab input box.
  3. Now you can access your Drive files directly via their path. For example:
    img = plt.imread("/content/drive/MyDrive/My_Images/sunset.jpg")
    

Fixing Common Errors

If you ran into issues before, here’s what’s likely going wrong:

  • FileNotFoundError: Double-check your filename spelling (case matters!) or use os.getcwd() to confirm your current working directory matches where the files are stored.
  • Permission Errors: Make sure you have read access to the local files you’re trying to upload—don’t try to upload system-protected files.
  • Upload Timeouts: For large images, compress them first (e.g., reduce resolution) or use the Drive mount method instead of direct upload.

内容的提问来源于stack exchange,提问作者Luis Ramon Ramirez Rodriguez

火山引擎 最新活动