(MJ转无界大模型篇)(公司炼大模型)对MJ数据的统一处理流程

1.【删除乱码并打标签】删前缀(用户名),删后缀(乱码),加统一标签,并打开excel微调。(输入项为1.单个文件夹地址 2.需要文件夹内加上的标签名)

*注意:此时若要加多个标签,请用英文逗号“,”隔开。

import os
import openpyxl
import re

UNWANTED_UNITS = ["undefined", "皮皮", "zly324"]


# 第一步:删名称
def rename_files(path):
    files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
    renamed_files = []

    counter = 1
    for file in files:
        filename, ext = os.path.splitext(file)

        # 乱码类
        if re.search(r'[a-f0-9]{32}', filename) or not '_' in filename:
            renamed = f"({counter})"
            counter += 1
        # AI出图类
        else:
            parts = re.split(r'[_]+', filename)
            parts.pop(0)  # 删除第一个单元

            # 删除特定的单元
            parts = [part for part in parts if part not in UNWANTED_UNITS]

            # 删除所有带数字的单元
            parts = [part for part in parts if not any(char.isdigit() for char in part)]

            # 结尾规则
            # 删除UUID风格数字
            while parts and re.search(r'^[a-f0-9\-]{32,}$', parts[-1]):
                parts.pop(-1)
            # 删除长度小于等于4的部分
            while parts and len(parts[-1]) <= 4:
                parts.pop(-1)

            renamed = '_'.join(parts)

        renamed_files.append(renamed + ext)

    return renamed_files


# 第二步:增名称
def add_prefix(files, prefix):
    prefixed_files = [f"{prefix}_{file}" if not file.startswith(prefix) else file for file in files]

    # 删除特定的单元
    prefixed_files = ['_'.join([part for part in re.split(r'[_]+', name) if part not in UNWANTED_UNITS]) for name in
                      prefixed_files]

    return prefixed_files


# 第三步:创建Excel并自动打开
def create_and_open_excel(files, renamed_files, path):
    wb = openpyxl.Workbook()
    ws = wb.active

    for original, renamed in zip(files, renamed_files):
        ws.append([original, renamed])

    excel_path = os.path.join(path, os.path.basename(path) + ".xlsx")
    wb.save(excel_path)

    # 打开Excel文件
    os.system(f'start "" "{excel_path}"')

    return excel_path


# 第五步:读取Excel并重命名文件
def rename_files_from_excel(path, excel_path):
    wb = openpyxl.load_workbook(excel_path)
    ws = wb.active

    for row in ws.iter_rows(values_only=True):
        original_name, new_name = row
        target_path = os.path.join(path, new_name)

        # 检查原文件是否存在
        if os.path.exists(os.path.join(path, original_name)):
            # 如果目标文件名已存在,则添加一个编号
            counter = 1
            base_name, ext = os.path.splitext(new_name)
            while os.path.exists(target_path):
                new_name = f"{base_name} ({counter}){ext}"
                target_path = os.path.join(path, new_name)
                counter += 1

            os.rename(os.path.join(path, original_name), target_path)

    print("重命名完成。")


# 主函数
def main():
    path = input("请输入文件夹地址: ")
    files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
    renamed_files = rename_files(path)

    prefix = input("请输入需要批量命名的词: ")
    prefixed_files = add_prefix(renamed_files, prefix)

    excel_path = create_and_open_excel(files, prefixed_files, path)
    print(f"Excel文件已保存为:{excel_path}")
    print("请在Excel里微调B列数据,然后保存和关闭Excel文件。完成后按Enter键继续...")

    input()

    # 重命名文件
    rename_files_from_excel(path, excel_path)


if __name__ == "__main__":
    main()

2.【处理下划线】统一只保留第一个下划线,删除后面的下划线(输入项为:总地址)

import os
import shutil

def copy_directory(src, dst):
    """复制 src 目录到 dst 目录。"""
    try:
        shutil.copytree(src, dst)
    except FileExistsError:
        print(f"备份目录 '{dst}' 已存在。")

def rename_image_files(directory):
    """重命名图片文件,保留第一个下划线,将其他下划线替换为空格,并在需要时添加递增编号。"""
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
                # 找到第一个下划线的位置
                first_underscore = file.find('_')
                if first_underscore != -1:
                    # 保留第一个下划线,将后续下划线替换为空格
                    before_first_underscore = file[:first_underscore + 1]
                    after_first_underscore = file[first_underscore + 1:].replace('_', ' ')
                    new_file_name = before_first_underscore + after_first_underscore
                else:
                    new_file_name = file

                if new_file_name != file:
                    original_file_path = os.path.join(root, file)
                    new_file_path = os.path.join(root, new_file_name)
                    increment = 1
                    # 循环直到找到不冲突的文件名
                    while os.path.exists(new_file_path):
                        # 分离文件名和扩展名
                        file_name, file_extension = os.path.splitext(new_file_name)
                        # 添加递增编号
                        new_file_name = f"{file_name} ({increment}){file_extension}"
                        new_file_path = os.path.join(root, new_file_name)
                        increment += 1
                    os.rename(original_file_path, new_file_path)
                    print(f"文件 {original_file_path} 已重命名为 {new_file_path}")

def main():
    # 请求用户输入要处理的目录路径
    input_directory = input("请输入要处理的目录路径: ")

    # 检查目录是否存在
    if not os.path.exists(input_directory):
        print(f"指定的目录 {input_directory} 不存在。")
        return

    # 创建目录的备份
    backup_directory = os.path.join(input_directory, "_backup")
    print(f"正在创建备份目录: {backup_directory}")
    copy_directory(input_directory, backup_directory)

    # 在备份目录中重命名图片文件
    print("正在重命名备份目录中的图片文件...")
    rename_image_files(backup_directory)
    print("重命名操作完成。")

if __name__ == "__main__":
    main()

 

3.【逗号转下划线】大地址下图片文件名所有逗号转换为下划线,适用于输入多条标签情况。(输入项为大地址)

import os

def replace_commas_in_filenames_interactive():
    """
    交互式地从用户处获取目录路径,然后遍历该路径及其子目录,
    并将图片文件名中的逗号替换为下划线。
    """
    path = input("请输入目录路径: ")
    modified_files = []
    # 支持的图片文件扩展名
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp']

    for root, dirs, files in os.walk(path):
        for file in files:
            if any(file.endswith(ext) for ext in image_extensions):
                # 替换逗号为下划线
                new_file = file.replace(',', '_').replace('', '_')
                if new_file != file:
                    os.rename(os.path.join(root, file), os.path.join(root, new_file))
                    modified_files.append(os.path.join(root, new_file))

    return modified_files

# 运行这个函数
modified_files = replace_commas_in_filenames_interactive()
for f in modified_files:
    print(f"Modified file: {f}")

 

4.【加特殊词区分底模数据】复刻第一个下划线前的内容,并加上WJ前缀。(输入项为大地址)。

如原来是unmbrella_xxx

变为WJunmbrella_umbrella_xxx

import os

def rename_images_in_directory(directory):
    # 定义识别为图片的文件扩展名
    image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff'}

    for root, dirs, files in os.walk(directory):
        for file in files:
            # 检查文件扩展名是否在定义的图片扩展名集合中
            if any(file.lower().endswith(ext) for ext in image_extensions):
                old_name = os.path.join(root, file)
                name_parts = file.split('_')
                if name_parts:
                    # 在第一个单元前添加前缀"WJ"
                    new_name = 'WJ' + name_parts[0].strip() + '_' + file
                    new_name = os.path.join(root, new_name)
                    # 重命名文件
                    os.rename(old_name, new_name)
                    print(f'Renamed: {old_name} to {new_name}')

# 提示输入目录路径
directory_path = input("请输入您想处理的目录路径: ")
rename_images_in_directory(directory_path)

 

posted @ 2023-11-14 16:43  不上火星不改名  阅读(146)  评论(0)    收藏  举报