CivArchive
    ← All articles
    Published November 25, 2024by angrysky

    Easy Save and Match Prompts to PNG for Civitai LoRA Training. Batch re-naming script and workflow.

    364 views1 reactions1 comments on CivitAI10 collected
    listhowtotool guidepromptingcomfyui workflowlora training

    How to Add Prompts to Images for Model Training on Civitai

    This guide will help you set up a workflow to save text prompts alongside images, making it easy to match prompts to images for model training using Civitai's LoRA maker. We'll use the Mnemic-Nodes' Save Text to File node and the WAS Node Suite's Text to String node in ComfyUI.


    Workflow Overview

    1. Text Input: Use the Text to String node to input your prompt.

    2. Text Encoding: Connect the output of the Text to String node to the CLIP Text Encode node (with text field set to input).

    3. Saving the Prompt: Connect the same output from the Text to String node to the Save Text to File node to save the prompt as a text file.

    4. Image Generation: Proceed with your image generation nodes as usual, using the encoded text from the CLIP Text Encode node.


    Step-by-Step Instructions

    1. Set Up the Nodes

    • Add the Text to String Node:

      • Locate the Text to String node from the WAS Node Suite in ComfyUI.

      • Drag it into your workspace.

    • Modify the CLIP Text Encode Node:

      • Add the CLIP Text Encode node to your workspace.

      • Switch Text Field to Input:

        • Click on the node to access its settings.

        • Change the text input mode to accept input from other nodes.

    • Add the Save Text to File Node:

      • Find the Save Text to File node from Mnemic-Nodes.

      • Add it to your workspace.

    2. Connect the Nodes

    • Connect Text to String to CLIP Text Encode:

      • Draw a connection from the output of the Text to String node to the input of the CLIP Text Encode node.

    • Connect Text to String to Save Text to File:

      • Draw another connection from the same output of the Text to String node to the input of the Save Text to File node.

    • Complete Your Workflow:

      • Continue building your image generation workflow, using the output from the CLIP Text Encode node as needed.

    3. Input Your Prompt and Generate Images

    • Enter Your Prompt:

      • Click on the Text to String node.

      • Type in your desired prompt.

    • Run the Workflow:

      • Execute the workflow to generate images.

      • The prompt will be saved as a text file alongside the image.


    Matching Prompts to Images with the Provided Script

    To prepare your images and prompts for model training using Civitai's LoRA maker, you need to ensure that the filenames of your images and prompt text files match. The following Python script renames your image and text files in a sequential order, making it easy for the LoRA maker to pair them correctly.

    Python Script: Rename Files Sequentially

    import os
    import shutil
    
    def rename_files(input_folder, output_folder, num_digits=3, start_index=0, copy_files=False):
        # Ensure output folder exists
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)
    
        # Collect all files from input folders
        all_files = []
        for input_folder in input_folder:
            # List all files in the input folder (excluding subfolders)
            files = [os.path.join(input_folder, f) for f in os.listdir(input_folder)
                     if os.path.isfile(os.path.join(input_folder, f))]
            files.sort()  # Sort files to maintain order
            all_files.extend(files)
    
        # Set up formatting for file names
        format_str = '{:0' + str(num_digits) + 'd}'  # e.g., '{:03d}' for zero-padding
    
        # Rename and copy/move files
        for idx, file_path in enumerate(all_files, start=start_index):
            filename = os.path.basename(file_path)
            extension = os.path.splitext(filename)[1]  # Get the file extension
            new_name = format_str.format(idx) + extension  # Create new filename with padding and extension
            new_file = os.path.join(output_folder, new_name)
    
            if copy_files:
                # Copy the file to the new location with the new name
                shutil.copy2(file_path, new_file)
            else:
                # Move the file to the new location with the new name
                shutil.move(file_path, new_file)
            print(f'Renamed "{filename}" to "{new_name}"')
    
    if __name__ == "__main__":
        # Specify the input folder paths and output folder path here
        input_folder = [
            r'F:/ComfyUI_flux/ComfyUI_windows_portable/ComfyUI/output/prompts1124'   
        ]
        output_folder = r'F:/ai_code_attempts/gpt4_codes/fileNameChanger/newNames1'
    
        # Parameters
        num_digits = 3       # Number of digits in the filename (e.g., 3 for '000')
        start_index = 100      # Starting index for numbering- If you don't change this or the folder the prior output will be overwritten
        copy_files = True    # Set to True to copy files; False to move files (Careful)
    
        rename_files(input_folder, output_folder, num_digits, start_index, copy_files)
    
    

    How to Use the Script

    1. Prepare Your Folders:

      • Images Folder: Contains all the images generated.

      • Prompts Folder: Contains the text files with prompts.

    2. Update the Script Paths:

      • Replace 'path\to\your\images_folder' with the path to your images folder.

      • Replace 'path\to\your\prompts_folder' with the path to your prompts folder.

      • Set the 'path\to\your\output_folder' to where you want the renamed files to be saved.

      • Change \ to /

    3. Adjust Script Parameters (Optional):

      • num_digits: Set the number of digits for zero-padding (e.g., 3 for filenames like 000.png).

      • start_index: Set the starting number for file naming.

      • copy_files: Set to True to copy files (keep originals), or False to move files.

    4. Run the Script:

      • Execute the script using Python 3.x.

      • The script will rename the files in both folders sequentially and place them in the output folder.

    Result

    • Renamed Files: Your images and prompt text files will be renamed to match sequentially (e.g., 000.png and 000.txt).

    • Ready for LoRA Maker: The LoRA maker on Civitai will now correctly match images to prompts based on the filenames.


    Conclusion

    By setting up your workflow to save prompts alongside images and using the provided script to rename files, you streamline the process of preparing data for model training on Civitai's LoRA maker. This ensures that your prompts and images are correctly paired, facilitating efficient and effective model training.

    Happy Training!

    P.S. I use VS Code to run the script easy.

    Attachments