【问题标题】:Google Colab: How To Read Multiple Images From a Google Drive in Google ColabGoogle Colab:如何在 Google Colab 中从 Google Drive 中读取多个图像
【发布时间】:2021-08-14 09:09:54
【问题描述】:
我想将 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
【问题讨论】:
标签:
python
jupyter-notebook
google-drive-api
google-colaboratory
【解决方案1】:
首先在您的 colab 实例上安装 google drive,然后执行此操作。
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
【解决方案2】:
我通过以下代码解决了这个问题
import os
from os import listdir
from os.path import isfile, join
images = {}
im2 = []
for root, dirs, files in os.walk("Main-Folder-Path"):
path = root.split(os.sep)
for index, file in enumerate(files):
im2 = [ f for f in listdir(root) if isfile(join(root,f)) ]
images[index] = join(root,im2[index])
现在它将逐个迭代每个文件夹
第一个 Sub_Folder_1 和 Sub_Folder_1 中的所有文件
& 之后所有的文件夹和文件