Skip to content

Instantly share code, notes, and snippets.

@amponce
Created March 12, 2025 20:57
Show Gist options
  • Save amponce/e05ea79701429cdfc72b7985a80b9490 to your computer and use it in GitHub Desktop.
Save amponce/e05ea79701429cdfc72b7985a80b9490 to your computer and use it in GitHub Desktop.

Dress Variation Generator - README

This tool allows you to generate hundreds of dress design variations using Stable Diffusion's image-to-image capabilities running locally on your PC.

Overview

This setup uses the Automatic1111 WebUI for Stable Diffusion, which provides an easy-to-use API that our Python script will call to generate dress variations based on your input images.

System Requirements

  • Windows 10/11, macOS, or Linux
  • NVIDIA GPU with at least 4GB VRAM (strongly recommended)
  • 16GB RAM recommended
  • Python 3.8 or newer
  • ~10GB free disk space for models and generated images

1. Setup Instructions

  1. Clone the Automatic1111 WebUI repository:

    git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
    cd stable-diffusion-webui
    
  2. Download a Stable Diffusion model:

    • Create a folder named models/Stable-diffusion inside the repository folder
    • Download one of these recommended models:
    • Place the downloaded model file in the models/Stable-diffusion folder
  3. Launch the WebUI with API enabled:

    • Windows: Double-click webui-user.bat, then close it and edit the file to add --api to the COMMANDLINE_ARGS
    • macOS/Linux: Run ./webui.sh --api
    • The first launch will take some time as it downloads additional components
  4. Verify the WebUI is running:

2. Set Up the Dress Variation Generator Script

  1. Create a project folder for the dress generator:

    mkdir dress_generator
    cd dress_generator
    
  2. Install required Python packages:

    pip install requests pillow
    
  3. Create the Python script:

    • Create a file named generate_dress_variations.py with the content from the script below
  4. Prepare your input image:

    • Place a high-quality image of a dress in the same folder
    • Name it dress_image.jpg
    • Ideally use fashion runway images with clear lighting and neutral backgrounds

3. Generate Dress Variations

  1. Make sure the Stable Diffusion WebUI is running with the API enabled

  2. Run the script:

    python generate_dress_variations.py
    
  3. Monitor progress:

    • The script will show generation progress in the console
    • Generated images will be saved in the dress_variations folder

Python Script

import requests
import json
import base64
import io
import os
from PIL import Image
import time
import random

# Configuration
sd_url = "http://127.0.0.1:7860"  # Default local WebUI address
input_image_path = "dress_image.jpg"  # Your reference dress image
output_folder = "dress_variations"
num_variations = 100  # Number of variations to generate

# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)

# Load and encode the input image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

input_image = encode_image(input_image_path)

# Parameters for image generation
params = {
    "init_images": [input_image],
    "prompt": "a beautiful dress on runway model, fashion show, high quality, detailed fabric",
    "negative_prompt": "low quality, blurry, distorted, deformed, bad anatomy, unrealistic proportions",
    "denoising_strength": 0.75,  # Adjust for more variation (higher) or closer to original (lower)
    "seed": -1,  # Random seed each time
    "batch_size": 1,
    "n_iter": 1,
    "steps": 30,
    "cfg_scale": 7.5,
    "width": 512,
    "height": 768,
    "sampler_name": "DPM++ 2M Karras"
}

# Style and color modifiers for variety
style_modifiers = [
    "floral pattern", "polka dot", "striped", "elegant", "vintage", 
    "modern", "summer", "formal", "casual", "couture", "embroidered",
    "lace", "silk", "satin", "cotton", "tulle", "sequined", "minimalist"
]

color_modifiers = [
    "red", "blue", "green", "yellow", "purple", "pink", "black", "white", 
    "teal", "orange", "navy", "burgundy", "emerald", "gold", "silver",
    "pastel", "multicolored", "ombre", "monochrome"
]

# Design elements for more variety
design_elements = [
    "sleeveless", "short sleeve", "long sleeve", "off-shoulder", "strapless",
    "v-neck", "high neck", "halter", "A-line", "fitted", "pleated", "tiered",
    "asymmetrical", "midi length", "maxi length", "mini length", "belted"
]

# Generate variations
for i in range(num_variations):
    print(f"Generating variation {i+1}/{num_variations}")
    
    # Randomize seed for variety
    params["seed"] = random.randint(-1, 2147483647)
    
    # Randomize denoising strength (how much to vary from original)
    params["denoising_strength"] = random.uniform(0.65, 0.85)
    
    # Choose random modifiers
    style = random.choice(style_modifiers)
    color = random.choice(color_modifiers)
    design = random.choice(design_elements)
    
    # Build varied prompt
    params["prompt"] = f"a beautiful {style} {color} {design} dress on runway model, fashion show, high quality, detailed fabric, professional fashion photography"
    
    # Occasionally add seasonal or occasion-specific modifiers
    if random.random() < 0.3:
        seasons = ["spring", "summer", "fall", "winter"]
        occasions = ["evening", "cocktail", "wedding", "casual", "office", "party"]
        extra = random.choice(seasons + occasions)
        params["prompt"] += f", {extra} wear"
    
    # Make API request
    try:
        response = requests.post(url=f'{sd_url}/sdapi/v1/img2img', json=params, timeout=120)
        
        if response.status_code == 200:
            r = response.json()
            
            # Save the generated image
            for j, image_data in enumerate(r['images']):
                image = Image.open(io.BytesIO(base64.b64decode(image_data)))
                image_filename = os.path.join(output_folder, f"dress_variation_{i+1}_{j+1}.png")
                image.save(image_filename)
                print(f"Saved: {image_filename}")
        else:
            print(f"Error: {response.status_code}")
            print(response.text)
    except Exception as e:
        print(f"Request error: {e}")
    
    # Add a small delay to prevent overwhelming the API
    time.sleep(1)

print("All variations generated!")

Troubleshooting

Common Issues

  1. WebUI not starting:

    • Check Python version (must be 3.8+)
    • For NVIDIA GPUs, ensure you have the latest graphics drivers
    • Try running with --skip-torch-cuda-test flag
  2. API connection refused:

    • Ensure WebUI is running with --api flag
    • Check that the URL in the script matches your WebUI address (default: http://127.0.0.1:7860)
  3. Out of memory errors:

    • Reduce image dimensions in the script (e.g., 512x512)
    • Reduce the steps parameter (20-30 is usually sufficient)
    • Use --medvram or --lowvram flags when starting the WebUI
  4. Slow generation:

    • Normal for CPU-only systems (consider using Google Colab instead)
    • Reduce steps parameter
    • Use faster samplers like "Euler a"

Additional Resources

Customization

You can modify the script parameters to:

  • Change the number of variations
  • Adjust how different each variation will be from the original
  • Modify the style, color, and design prompts
  • Change the output image dimensions and quality

For more advanced customization, explore ControlNet extensions in the WebUI, which allow for more precise control over pose, layout, and design elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment