File size: 1,831 Bytes
044761a b32617e 044761a b32617e 044761a |
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 |
import os
import sys
import tqdm
import utils
import random
import argparse
from constants import *
def parse_args():
parser = argparse.ArgumentParser(description="Convert JSON image metadata to TXT image tags.")
parser.add_argument("-n", "--no-delete", action="store_true", help="If set, won't delete the JSON image metadata files")
mutex = parser.add_mutually_exclusive_group()
mutex.add_argument("-e", "--exclude", nargs="+", help="Exclude tag groups with the specified group names, you can only set either exclude or include, but not both")
mutex.add_argument("-i", "--include", nargs="+", help="Include tag groups with the specified group names, you can only set either include or exclude, but not both")
parser.add_argument("-p", "--no-rating-prefix", action="store_true", help="If set, won't prepend the \"rating:\" prefix to the rating")
return parser.parse_args()
def main():
args = parse_args()
print("Starting...\nGetting paths...")
image_id_image_metadata_path_tuple_dict = utils.get_image_id_image_metadata_path_tuple_dict(IMAGE_DIR)
print("Got", len(image_id_image_metadata_path_tuple_dict), "images.")
for _, metadata_path in tqdm.tqdm(image_id_image_metadata_path_tuple_dict.values(), desc="Converting"):
tags = utils.get_tags(metadata_path, args.exclude, args.include, args.no_rating_prefix)
random.shuffle(tags)
tags_text = ", ".join(tag.replace("_", " ") for tag in tags)
with open(os.path.splitext(metadata_path)[0] + ".txt", "w", encoding="utf8") as tags_file:
tags_file.write(tags_text)
if not args.no_delete:
os.remove(metadata_path)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nScript interrupted by user, exiting...")
sys.exit(1)
|