Skip to content

Commit

Permalink
fix paths.py issue
Browse files Browse the repository at this point in the history
  • Loading branch information
skypanther committed Oct 26, 2018
1 parent d277bb7 commit 543114f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Binary file added demo_images/folder with spaces/bridge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions demos/image_paths.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 8 additions & 4 deletions imutils/paths.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 543114f

Please sign in to comment.