$npx -y skills add AlexAI-MCP/hermes-CCC --skill stable-diffusionRun Stable Diffusion locally with diffusers — text-to-image, img2img, inpainting, ControlNet, and SDXL.
| 1 | # Stable Diffusion |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Use this skill to generate or edit images locally with Hugging Face `diffusers`. |
| 6 | - Prefer it for text-to-image, img2img, inpainting, and model composition workflows. |
| 7 | - This skill covers both classic Stable Diffusion models and SDXL. |
| 8 | - It is useful for scripted generation, reproducible experiments, and GPU-backed image pipelines. |
| 9 | |
| 10 | ## Install |
| 11 | |
| 12 | ```bash |
| 13 | pip install diffusers transformers accelerate torch |
| 14 | ``` |
| 15 | |
| 16 | - You typically also need a compatible CUDA-enabled PyTorch build for GPU inference. |
| 17 | - Confirm the install in Python before pulling large checkpoints. |
| 18 | |
| 19 | ## Core Libraries |
| 20 | |
| 21 | - `diffusers` for pipeline abstractions |
| 22 | - `transformers` for text encoders and related model components |
| 23 | - `accelerate` for efficient device loading and memory movement |
| 24 | - `torch` for runtime execution |
| 25 | |
| 26 | ## Text-to-Image With SD 1.5 |
| 27 | |
| 28 | - Classic Stable Diffusion 1.5 uses `StableDiffusionPipeline`. |
| 29 | |
| 30 | ```python |
| 31 | import torch |
| 32 | from diffusers import StableDiffusionPipeline |
| 33 | |
| 34 | pipe = StableDiffusionPipeline.from_pretrained( |
| 35 | "runwayml/stable-diffusion-v1-5", |
| 36 | torch_dtype=torch.float16, |
| 37 | ) |
| 38 | pipe = pipe.to("cuda") |
| 39 | |
| 40 | image = pipe( |
| 41 | prompt="a cinematic photo of a mountain observatory at sunrise", |
| 42 | negative_prompt="blurry, low quality, distorted", |
| 43 | num_inference_steps=30, |
| 44 | guidance_scale=7.5, |
| 45 | ).images[0] |
| 46 | |
| 47 | image.save("output.png") |
| 48 | ``` |
| 49 | |
| 50 | ## SDXL |
| 51 | |
| 52 | - SDXL typically uses `StableDiffusionXLPipeline`. |
| 53 | |
| 54 | ```python |
| 55 | import torch |
| 56 | from diffusers import StableDiffusionXLPipeline |
| 57 | |
| 58 | pipe = StableDiffusionXLPipeline.from_pretrained( |
| 59 | "stabilityai/stable-diffusion-xl-base-1.0", |
| 60 | torch_dtype=torch.float16, |
| 61 | ) |
| 62 | pipe = pipe.to("cuda") |
| 63 | |
| 64 | image = pipe( |
| 65 | prompt="a highly detailed editorial photo of a futuristic library interior", |
| 66 | negative_prompt="low resolution, deformed, extra limbs", |
| 67 | num_inference_steps=35, |
| 68 | guidance_scale=6.5, |
| 69 | ).images[0] |
| 70 | |
| 71 | image.save("sdxl-output.png") |
| 72 | ``` |
| 73 | |
| 74 | - SDXL generally produces stronger prompt fidelity and image quality than SD 1.5, but it also requires more VRAM. |
| 75 | |
| 76 | ## Key Parameters |
| 77 | |
| 78 | - `prompt`: the main text instruction |
| 79 | - `negative_prompt`: what to suppress |
| 80 | - `num_inference_steps`: denoising step count |
| 81 | - `guidance_scale`: classifier-free guidance strength |
| 82 | - `seed`: random seed for reproducibility |
| 83 | |
| 84 | ## Seeded Generation |
| 85 | |
| 86 | - Use a seed when you need repeatable outputs: |
| 87 | |
| 88 | ```python |
| 89 | import torch |
| 90 | from diffusers import StableDiffusionPipeline |
| 91 | |
| 92 | pipe = StableDiffusionPipeline.from_pretrained( |
| 93 | "runwayml/stable-diffusion-v1-5", |
| 94 | torch_dtype=torch.float16, |
| 95 | ).to("cuda") |
| 96 | |
| 97 | generator = torch.Generator(device="cuda").manual_seed(42) |
| 98 | |
| 99 | image = pipe( |
| 100 | prompt="a clean product photo of a ceramic mug on a wood table", |
| 101 | negative_prompt="blurry, noisy, warped", |
| 102 | num_inference_steps=28, |
| 103 | guidance_scale=7.0, |
| 104 | generator=generator, |
| 105 | ).images[0] |
| 106 | |
| 107 | image.save("seeded-output.png") |
| 108 | ``` |
| 109 | |
| 110 | - The same seed and settings help with debugging prompt and LoRA changes. |
| 111 | |
| 112 | ## Save Output |
| 113 | |
| 114 | - Save the generated image with PIL: |
| 115 | |
| 116 | ```python |
| 117 | image.save("output.png") |
| 118 | ``` |
| 119 | |
| 120 | - Always save prompt metadata separately if you need auditability or experiment tracking. |
| 121 | |
| 122 | ## Img2Img |
| 123 | |
| 124 | - Use `StableDiffusionImg2ImgPipeline` to transform an existing image while preserving composition. |
| 125 | |
| 126 | ```python |
| 127 | import torch |
| 128 | from diffusers import StableDiffusionImg2ImgPipeline |
| 129 | from PIL import Image |
| 130 | |
| 131 | pipe = StableDiffusionImg2ImgPipeline.from_pretrained( |
| 132 | "runwayml/stable-diffusion-v1-5", |
| 133 | torch_dtype=torch.float16, |
| 134 | ).to("cuda") |
| 135 | |
| 136 | init_image = Image.open("input.png").convert("RGB").resize((768, 768)) |
| 137 | |
| 138 | image = pipe( |
| 139 | prompt="turn this concept sketch into a polished sci-fi matte painting", |
| 140 | negative_prompt="blurry, low contrast, artifacts", |
| 141 | image=init_image, |
| 142 | strength=0.65, |
| 143 | num_inference_steps=30, |
| 144 | guidance_scale=7.5, |
| 145 | ).images[0] |
| 146 | |
| 147 | image.save("img2img-output.png") |
| 148 | ``` |
| 149 | |
| 150 | - Lower `strength` preserves more of the input image. |
| 151 | - Higher `strength` pushes the result further away from the source. |
| 152 | |
| 153 | ## Inpainting |
| 154 | |
| 155 | - Use `StableDiffusionInpaintPipeline` to replace or repair masked regions. |
| 156 | |
| 157 | ```python |
| 158 | import torch |
| 159 | from diffusers import StableDiffusionInpaintPipeline |
| 160 | from PIL import Image |
| 161 | |
| 162 | pipe = StableDiffusionInpaintPipeline.from_pretrained( |
| 163 | "runwayml/stable-diffusion-inpainting", |
| 164 | torch_dtype=torch.float16, |
| 165 | ).to("cuda") |
| 166 | |
| 167 | image = Image.open("scene.png").convert("RGB").resize((512, 512)) |
| 168 | mask = Image.open("mask.png").convert("RGB").resize((512, 512)) |
| 169 | |
| 170 | result = pipe( |
| 171 | prompt="replace the missing area with a wooden chair", |
| 172 | negative_prompt="blurry, malformed, duplicate objects", |
| 173 | image=image, |
| 174 | mask_image=mask, |
| 175 | num_inference_steps=30, |
| 176 | guidance_scale=7.5, |
| 177 | ).images[0] |
| 178 | |
| 179 | res |