Skip to content

Commit 4476b8f

Browse files
committed
feat: add glob pattern support to tifcompress.py
- Import `glob` and use `glob.glob()` to expand wildcard arguments (e.g., *.TIF) on Windows where the shell does not perform expansion. - Refactor file discovery logic to handle both glob patterns and literal paths. - Add clearer error handling for missing paths and a check for “No TIFF files found.” - Restore strategy selection (`overwrite`, `pool`, `subfolder`) after file discovery.
1 parent 115a812 commit 4476b8f

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

Programming/tifcompress.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import threading
4646
import multiprocessing
4747
import shutil
48+
import glob
4849
from PIL import Image, __version__ as pil_version
4950
from PIL import features
5051
from pathlib import Path
@@ -286,25 +287,48 @@ def main():
286287
print(f"Pillow: {pil_version} | LibTIFF: {features.check('libtiff')}")
287288
print(f"Deflate: {features.check('zlib')}")
288289

289-
input_base = Path(args.path).resolve()
290-
if not input_base.exists(): sys.exit(1)
291-
292-
selected_strategy = "overwrite"
293-
if args.pool: selected_strategy = "pool"
294-
elif args.subfolder: selected_strategy = "subfolder"
295-
290+
input_path = args.path
291+
292+
# Expand glob patterns (Windows doesn't expand wildcards in shell)
293+
expanded_paths = glob.glob(input_path)
294+
296295
files_to_process = []
297-
if input_base.is_file():
298-
if input_base.suffix.lower() in ALLOWED_EXTENSIONS: files_to_process.append(input_base)
296+
297+
if expanded_paths:
298+
# Glob matched something - process each match
299+
for p in expanded_paths:
300+
path_obj = Path(p).resolve()
301+
if path_obj.is_file() and path_obj.suffix.lower() in ALLOWED_EXTENSIONS:
302+
files_to_process.append(path_obj)
303+
elif path_obj.is_dir():
304+
for root, _, files in os.walk(path_obj):
305+
for f in files:
306+
if f.lower().endswith(ALLOWED_EXTENSIONS):
307+
files_to_process.append(Path(root) / f)
299308
else:
300-
for root, _, files in os.walk(input_base):
301-
for f in files:
302-
if f.lower().endswith(ALLOWED_EXTENSIONS): files_to_process.append(Path(root) / f)
309+
# No glob match - treat as literal path
310+
input_base = Path(input_path).resolve()
311+
if not input_base.exists():
312+
print(f"Path not found: {input_path}")
313+
sys.exit(1)
314+
315+
if input_base.is_file():
316+
if input_base.suffix.lower() in ALLOWED_EXTENSIONS:
317+
files_to_process.append(input_base)
318+
else:
319+
for root, _, files in os.walk(input_base):
320+
for f in files:
321+
if f.lower().endswith(ALLOWED_EXTENSIONS):
322+
files_to_process.append(Path(root) / f)
303323

304324
if not files_to_process:
305325
print("No TIFF files found.")
306326
return
307327

328+
selected_strategy = "overwrite"
329+
if args.pool: selected_strategy = "pool"
330+
elif args.subfolder: selected_strategy = "subfolder"
331+
308332
sys_cores = os.cpu_count() or 1
309333
max_workers = int(args.jobs) if args.jobs != "auto" else sys_cores
310334

0 commit comments

Comments
 (0)