简体   繁体   中英

Google Colab: How To Read Multiple Images From a Google Drive in Google Colab

I Want to Read Multiple Image Files From The Google Drive into My Google Colab Python Notebook Below is My Google Drive Folder Structure

--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

First mount google drive on your colab instance and then do this.

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

I Solved That Issue By The Following Code

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])

Now It Will Iterate Each Folder One By One

1st Sub_Folder_1 & All The Files in Sub_Folder_1

& After That All the Folders & Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM