Skip to content

Commit

Permalink
Add the recursive search option
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglongMa committed Nov 17, 2023
1 parent bfaf68e commit c951e3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/stone/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def main():
datefmt="%H:%M:%S",
)

image_paths = build_image_paths(args.images)
image_paths = build_image_paths(args.images, args.recursive)

debug: bool = args.debug
to_bw: bool = args.black_white
Expand Down
19 changes: 15 additions & 4 deletions src/stone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ArgumentError(ValueError):
"""
Wrapper for argument error. This exception will be raised when the arguments are invalid.
"""

pass


Expand Down Expand Up @@ -53,12 +54,15 @@ def extract_filename_and_extension(url):
return basename, f".{extension[0]}" if extension else None


def build_image_paths(images):
def build_image_paths(images_paths, recursive=False):
filenames, urls = [], []
valid_images = ["*.jpg", "*.gif", "*.png", "*.jpeg", "*.webp", "*.tif"]
for name in images:
if isinstance(images_paths, str):
images_paths = [images_paths]
recursive_flag = "**" if recursive else ""
for name in images_paths:
if os.path.isdir(name):
filenames.extend([glob.glob(os.path.join(name, "./**/", i), recursive=True) for i in valid_images])
filenames.extend([glob.glob(os.path.join(name, recursive_flag, i), recursive=recursive) for i in valid_images])
elif os.path.isfile(name):
filenames.append([name])
elif is_url(name):
Expand Down Expand Up @@ -94,14 +98,21 @@ def build_arguments():
"-i",
"--images",
nargs="+",
default="./",
default=["./"],
metavar="IMAGE FILENAME",
help="Image filename(s) or URLs to process;\n"
'Supports multiple values separated by space, e.g., "a.jpg b.png";\n'
'Supports directory or file name(s), e.g., "./path/to/images/ a.jpg";\n'
'Supports URL(s), e.g., "https://example.com/images/pic.jpg" since v1.1.0+.\n'
"The app will search all images in current directory in default.",
)
parser.add_argument(
"-r",
"--recursive",
action="store_true",
help="Whether to search images recursively in the specified directory.",
)

parser.add_argument(
"-t",
"--image_type",
Expand Down

0 comments on commit c951e3c

Please sign in to comment.