Google Colab:如何在 Google Colab 中从 Google Drive 读取多个图像

时间:2021-05-26 06:26:16

标签: python jupyter-notebook google-drive-api google-colaboratory

<块引用>

我想将 Google Drive 中的多个图像文件读入我的 Google Colab Python Notebook 下面是我的 Google Drive 文件夹结构

--Drive

  --Main_Folder

    --Sub_Folder_1

      --imagefile1.jpg
      --imagefile2.jpg
      --imagefile3.jpg

    --Sub_Folder_2

      --imagefile1.jpg
      --imagefile2.jpg
      --imagefile3.jpg

    --Sub_Folder_3

      --imagefile1.jpg
      --imagefile2.jpg
      --imagefile3.jpg

1 个答案:

答案 0 :(得分:0)

首先在您的 colab 实例上安装谷歌驱动器,然后执行此操作。

import os
from PIL import Image

info = []
for root, __, files in os.walk("path/to/google/drive/folder"):
  for f in files:
      if f.endswith(".jpg"):
         info.append({
                     "img": Image.open(os.path.join(root, f)), # add an appropriate reading flag if you want
                     # optional
                     # "foldername": os.path.dirname(root)
                     })

# do whatever you want with the infolist you have now