Last active
March 5, 2019 19:50
-
-
Save jrosebr1/82a4cf4266fb96dd3158 to your computer and use it in GitHub Desktop.
Image path generator for Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Adrian Rosebrock | |
# Website: www.pyimagesearch.com | |
# import the necessary packages | |
import os | |
def list_images(basePath, contains=None): | |
# return the set of files that are valid | |
return list_files(basePath, validExts=(".jpg", ".jpeg", ".png"), contains=contains) | |
def list_files(basePath, validExts=(".jpg", ".jpeg", ".png"), contains=None): | |
# loop over the directory structure | |
for (rootDir, dirNames, filenames) in os.walk(basePath): | |
# loop over the filenames in the current directory | |
for filename in filenames: | |
# if the contains string is not none and the filename does not contain | |
# the supplied string, then ignore the file | |
if contains is not None and filename.find(contains) == -1: | |
continue | |
# determine the file extension of the current file | |
ext = filename[filename.rfind("."):].lower() | |
# check to see if the file is an image and should be processed | |
if ext.endswith(validExts): | |
# construct the path to the image and yield it | |
imagePath = os.path.join(rootDir, filename).replace(" ", "\\ ") | |
yield imagePath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment