4

I've copied a large number of .jpg images from my Google Drive to Google Colab per Google Colab: how to read data from my google drive?:

local_download_path = os.path.expanduser('~/data')
try:
  os.makedirs(local_download_path)
except: pass

# 2. Auto-iterate using the query syntax
#    https://developers.google.com/drive/v2/web/search-parameters
file_list = drive.ListFile(
    {'q': "'1SooKSw8M4ACbznKjnNrYvJ5wxuqJ-YCk' in parents"}).GetList()

for f in file_list:
  # 3. Create & download by id.
  print('title: %s, id: %s' % (f['title'], f['id']))
  fname = os.path.join(local_download_path, f['title'])
  print('downloading to {}'.format(fname))
  f_ = drive.CreateFile({'id': f['id']})
  f_.GetContentFile(fname)

This completed, so all the image files should be in a data folder. How do I actually access these files now? I'd like to loop through all files that are of type .jpg in the data folder.

Thomas Sablik
  • 15,040
  • 7
  • 26
  • 51
ZhouW
  • 1,017
  • 1
  • 15
  • 33

3 Answers3

9

One alternative would be to use the already implemented os library.

# Just needed in case you'd like to append it to an array
data = []

for filename in os.listdir(local_download_path):
    if filename.endswith("jpg"): 
        # Your code comes here such as 
        print(filename)
        data.append(filename)
RandomDude90
  • 123
  • 2
3

Once you have your data downloaded into local_download_path you can use glob.glob to get files by extension:

import glob
images = glob.glob(local_download_path + '/*.jpg')
taras
  • 5,216
  • 9
  • 32
  • 41
2

Get all files and convert to numpy array

import cv2
import glob
path = "/content/bb/*.*"
for file in glob.glob(path):
   print(file)
   a= cv2.imread(file)
   print(a)
BIPUL MANDOL
  • 53
  • 1
  • 7