diff --git a/demo_images/folder with spaces/bridge.jpg b/demo_images/folder with spaces/bridge.jpg new file mode 100644 index 0000000..9ab99cd Binary files /dev/null and b/demo_images/folder with spaces/bridge.jpg differ diff --git a/demos/image_paths.py b/demos/image_paths.py index ae85601..a79946e 100644 --- a/demos/image_paths.py +++ b/demos/image_paths.py @@ -1,11 +1,33 @@ # author: Adrian Rosebrock # website: http://www.pyimagesearch.com +# Updated by Tim Poulsen, @skypanther + +# USAGE +# from the demos folder: +# python image_paths.py # import the necessary packages from __future__ import print_function +# You'll need to force '../imutils' into the system path if testing +# before this addition is merged into the imutils library so that you +# load the local package not the one installed on your system. +# sys.path.insert(0, '../imutils') +# import sys from imutils import paths # loop over the image paths in the previous 'demo_images' # directory and print the paths to the terminal for imagePath in paths.list_images("../demo_images"): print(imagePath) + +print('\nUsing `list_files` with a jpg file extension filter:\n') + +# validExts must be a tuple +for imagePath in paths.list_files("../demo_images", validExts=('.jpg',)): + print(imagePath) + +print('\nUsing `list_files` with a contains filter:\n') + +# validExts must be a tuple +for imagePath in paths.list_files("../demo_images", contains='bridge'): + print(imagePath) diff --git a/imutils/paths.py b/imutils/paths.py index 8bcdf79..5e60640 100644 --- a/imutils/paths.py +++ b/imutils/paths.py @@ -1,11 +1,15 @@ # import the necessary packages import os +image_types = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff") + + def list_images(basePath, contains=None): # return the set of files that are valid - return list_files(basePath, validExts=(".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"), contains=contains) + return list_files(basePath, validExts=image_types, contains=contains) + -def list_files(basePath, validExts=(".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"), contains=None): +def list_files(basePath, validExts=None, contains=None): # loop over the directory structure for (rootDir, dirNames, filenames) in os.walk(basePath): # loop over the filenames in the current directory @@ -19,7 +23,7 @@ def list_files(basePath, validExts=(".jpg", ".jpeg", ".png", ".bmp", ".tif", ".t ext = filename[filename.rfind("."):].lower() # check to see if the file is an image and should be processed - if ext.endswith(validExts): + if validExts is None or ext.endswith(validExts): # construct the path to the image and yield it - imagePath = os.path.join(rootDir, filename).replace(" ", "\\ ") + imagePath = os.path.join(rootDir, filename) yield imagePath