@jeremy I implemented a python function reindex(dest, start_idx=0, ext = None)
that takes a directory or directory of directories and uniquely reindexes all of the files across all directories. Optionally, a starting index and extension can be provided. Importantly, when it reindexes, it will save the non-numeric stem of the file name to the extent that was used for labeling. I plan to wrap it in a script so it can be used from either the shell or within Jupyter and post it on github.
image_dir contains directories of images: dir1/{1.jpg, 2.jpg, 3.jpg}, dir2/{1.jpg, 2.jpg, 3.jpg}, dir3/{1.jpg, 2.jpg, 3.jpg}
usage: reindex(image_dir) -> dir1/{1.jpg, 2.jpg, 3.jpg}, dir2/{4.jpg, 5.jpg, 6.jpg}, dir3/{7.jpg, 8.jpg, 9.jpg}
def reindex_dir(dest, start_idx=0, ext = None):
dest = Path(dest)
rnd = "zsk#@m"
fns,idx = [], None
for idx, fn in enumerate(filter(Path.is_file, dest.iterdir()), start=start_idx):
suf = ext if ext else fn.suffix.strip('.')
stm = re.compile('[0-9]*$').sub("",fn.stem.strip('.')) # remove numeric idx from end of stem
fn_new = dest/(f'{stm}{idx}.{suf}')
fn_tmp = dest/(str(idx) + rnd) # Need unique fn to avoid collisions
fns.append([fn_tmp,fn_new])
fn.rename(fn_tmp)
for fn_tmp, fn_new in fns:
fn_tmp.rename(fn_new)
return idx + 1 if idx else 0
def reindex(dest, start_idx=0, ext = None):
dest = Path(dest)
if not(dest.is_dir()): return f'{dest} is not a directory'
idx = start_idx
for d in filter(Path.is_dir, dest.iterdir()):
idx = reindex_dir(d, idx, ext=ext)