Skip to content

Instantly share code, notes, and snippets.

@gustavofuhr
Created July 12, 2024 20:51
Show Gist options
  • Select an option

  • Save gustavofuhr/99191ed55a4a298efcf10d2d169ede90 to your computer and use it in GitHub Desktop.

Select an option

Save gustavofuhr/99191ed55a4a298efcf10d2d169ede90 to your computer and use it in GitHub Desktop.
Get all the image files from a dir path, including subdirs.
import os
import glob
import functools, operator
# extract image paths inside the image_dir_path including may include subdirectories using glob
accepted_exts = [".jpg", ".jpeg", ".png"]
image_files = [glob.glob(os.path.join(image_dir_path, "*"+ex)) for ex in accepted_exts]
if include_subdirs:
image_files.extend([glob.glob(os.path.join(image_dir_path + "/**/", "*"+ex)) for ex in accepted_exts])
image_files = functools.reduce(operator.iconcat, image_files, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment