File size: 3,085 Bytes
ddb7519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import os
import glob
import argparse

def get_parser(**parser_kwargs):
    parser = argparse.ArgumentParser(**parser_kwargs)
    parser.add_argument(
        "--img_dir",
        type=str,
        nargs="?",
        const=True,
        default="input",
        help="directory with images to be renamed",
    ),
    parser.add_argument(
        "--find",
        type=str,
        nargs="?",
        const=True,
        default=None,
        help="what strings to replace, in csv format, default: 'a man,a woman,a person'",
    ),
    parser.add_argument(
        "--replace",
        type=str,
        nargs="?",
        required=False,
        const=True,
        default=None,
        help="string to replace with, ex. 'john doe'",
    ),
    parser.add_argument(
        "--append_only",
        type=str,
        nargs="?",
        required=False,
        const=True,
        default=None,
        help="skips pronoun replace, adds a string at the end of the filename, use for 'by artist name' or 'in the style of somestyle'",
    )
    
    return parser

def isWindows():
    return sys.platform.startswith('win')

def get_replace_list(opt):
    if opt.find is None:
        return ("a man", "a woman", "a person", \
            "a girl", "a boy", \
            "a young woman", "a young man", \
            "a beautiful woman", "a handsome man", \
            "a beautiful young woman", "a handsome young man",        
        )
    else:
        return opt.find.split(",")

def get_outfile_name(infile, append):
    new_filename = f"{os.path.splitext(infile)[0]} {append}{os.path.splitext(infile)[1]}"
    return new_filename

def rename_files(opt):
    find_list = get_replace_list(opt)

    for idx, file in enumerate(glob.iglob(f"{opt.img_dir}/*")):
        print(file)

        if os.path.splitext(file)[1] in (".jpg", ".png", ".jpeg", ".gif", ".bmp", ".webp"):
            new_filename = file
            if opt.append_only is not None:
                new_filename = get_outfile_name(file, opt.append_only)
            else:
                for s in find_list:
                    if s in file:
                        new_filename = new_filename.replace(s, opt.replace)
            try:
                print(f"Renaming {file} to {new_filename}")
                if os.path.exists(new_filename):
                    new_filename = new_filename = get_outfile_name(file, f"_{idx}")
                    print(f"filename already exists, appended '_n' to {new_filename}")

                try:
                    os.rename(file, new_filename)
                except Exception as e:
                    print(f"Error renaming file: {file}, skipping, error: {e}")
            except Exception as e:
                print(f"error opening file: {file}")
                print(f"{e}")
                raise e


if __name__ == "__main__":
    parser = get_parser()
    opt = parser.parse_args()
    
    import time

    s = time.perf_counter()

    rename_files(opt)

    elapsed = time.perf_counter() - s
    print(f"{__file__} executed in {elapsed:0.2f} seconds.")