add script to push filename to .txt

This commit is contained in:
Victor Hall 2022-11-13 23:37:42 -05:00
parent 36a4fdc0e6
commit fa0287be84
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
import glob
import os
import argparse
def create_txt_from_filename(path):
"""
create a .txt file for each file in the path so you can lengthen the caption
"""
print(f"Creating .txt files from filenames in {path}")
for idx, f in enumerate(glob.iglob(f"{path}/**", recursive=True)):
print(f"Creating {f}.txt")
if not os.path.isfile(f) or not os.path.splitext(f)[1] in ['.jpg', '.png', '.jpeg', '.webp', '.bmp']:
continue
path_without_filename = os.path.dirname(f)
base_name = os.path.splitext(os.path.basename(f))[0]
caption = os.path.splitext(base_name)[0].split("_")[0]
target = f"{path_without_filename}/{base_name}.txt"
print (f"Creating file: {target} from {f}")
with open(target, "w") as text_file:
text_file.write(caption)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--path", type=str, help="path to folder")
args = parser.parse_args()
create_txt_from_filename(args.path)