Google Colab:如何循环浏览文件夹中的图像?

时间:2018-08-15 11:01:33

标签: python

我已Google Colab: how to read data from my google drive?将大量.jpg图像从Google云端硬盘复制到Google Colab:

class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        AlertView.show(title: "Alert", message: "Are you sure ?", preferredStyle: .alert, buttons: ["Yes","No"]) { (button) in
            print(button)
        }
    }

}

此操作已完成,因此所有图像文件都应位于# config/packages/fos_ck_editor.yaml fos_ck_editor: default_config: my_config configs: my_config: toolbar: [ ['Bold', 'Italic', 'Underline', 'NumberedList', 'BulletedList', 'Outdent', 'Indent'] ] extraPlugins: 'wordcount,notification' wordcount: showWordCount: false # disable this showCharCount: true # enable this plugins: wordcount: path: '/bundles/fosckeditor/plugins/wordcount/' filename: 'plugin.js' notification: path: '/bundles/fosckeditor/plugins/notification/' filename: 'plugin.js' 文件夹中。我现在该如何实际访问这些文件?我想遍历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) 文件夹中.jpg类型的所有文件。

3 个答案:

答案 0 :(得分:2)

一种选择是使用已经实现的os库。

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

答案 1 :(得分:1)

将数据下载到local_download_path后,您可以 使用glob.glob按扩展名获取文件:

import glob
images = glob.glob(local_download_path + '/*.jpg')

答案 2 :(得分:0)

获取所有文件并转换为numpy数组

import cv2
import glob
path = "/content/bb/*.*"
for file in glob.glob(path):
   print(file)
   a= cv2.imread(file)
   print(a)