Implementation:AUTOMATIC1111 Stable diffusion webui StableDiffusionProcessingTxt2Img
| Knowledge Sources | |
|---|---|
| Domains | Diffusion Models, Image Generation, Parameter Tuning |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for configuring and executing text-to-image generation with all relevant parameters, provided by the AUTOMATIC1111 stable-diffusion-webui repository.
Description
StableDiffusionProcessingTxt2Img is a Python dataclass that extends StableDiffusionProcessing to encapsulate the full parameter space for text-to-image generation in Stable Diffusion. It holds all user-configurable generation parameters (prompt, seed, dimensions, CFG scale, sampler, etc.) and adds hires fix support through additional fields (enable_hr, hr_scale, hr_upscaler, etc.).
The class implements the sample() method which orchestrates the complete generation flow:
- Creates the sampler from
sampler_name - Generates initial noise via
self.rng.next() - Calls the sampler to denoise the latent
- If hires fix is enabled, delegates to
sample_hr_pass()for a second denoising pass - Returns either raw latent samples or decoded samples
It also implements init() for hires-fix setup, setup_prompts() for prompt/HR prompt initialization, and calculate_target_resolution() for computing upscaled dimensions.
Usage
This class is instantiated by the WebUI's txt2img tab handler or the API endpoint. Users configure parameters through the UI, which are passed as constructor arguments to create a processing object. The object is then passed to process_images() which calls init(), sample(), and post-processing methods.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/processing.py - Lines: 1165-1554
Signature
@dataclass(repr=False)
class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
enable_hr: bool = False
denoising_strength: float = 0.75
firstphase_width: int = 0
firstphase_height: int = 0
hr_scale: float = 2.0
hr_upscaler: str = None
hr_second_pass_steps: int = 0
hr_resize_x: int = 0
hr_resize_y: int = 0
hr_checkpoint_name: str = None
hr_sampler_name: str = None
hr_scheduler: str = None
hr_prompt: str = ''
hr_negative_prompt: str = ''
force_task_id: str = None
Inherited from StableDiffusionProcessing:
@dataclass(repr=False)
class StableDiffusionProcessing:
sd_model: object = None
outpath_samples: str = None
outpath_grids: str = None
prompt: str = ""
negative_prompt: str = ""
styles: list[str] = None
seed: int = -1
subseed: int = -1
subseed_strength: float = 0
seed_resize_from_h: int = -1
seed_resize_from_w: int = -1
sampler_name: str = None
scheduler: str = None
batch_size: int = 1
n_iter: int = 1
steps: int = 50
cfg_scale: float = 7.0
width: int = 512
height: int = 512
restore_faces: bool = None
tiling: bool = None
do_not_save_samples: bool = False
do_not_save_grid: bool = False
eta: float = None
denoising_strength: float = None
s_min_uncond: float = None
s_churn: float = None
s_tmax: float = None
s_tmin: float = None
s_noise: float = None
override_settings: dict[str, Any] = None
refiner_checkpoint: str = None
refiner_switch_at: float = None
disable_extra_networks: bool = False
Import
from modules.processing import StableDiffusionProcessingTxt2Img
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sd_model | object | No | The loaded Stable Diffusion model; defaults to the globally loaded model |
| prompt | str | No | The positive text prompt describing the desired image; default "" |
| negative_prompt | str | No | The negative text prompt describing what to avoid; default "" |
| seed | int | No | Random seed for noise initialization; -1 for random; default -1 |
| width | int | No | Output image width in pixels (multiple of 8); default 512 |
| height | int | No | Output image height in pixels (multiple of 8); default 512 |
| steps | int | No | Number of sampling/denoising steps; default 50 |
| cfg_scale | float | No | Classifier-free guidance scale; default 7.0 |
| sampler_name | str | No | Name of the sampling algorithm (e.g., "Euler a", "DPM++ 2M Karras") |
| scheduler | str | No | Noise schedule type (e.g., "Karras", "Exponential", "Automatic") |
| batch_size | int | No | Number of images per batch; default 1 |
| n_iter | int | No | Number of batches to generate; default 1 |
| enable_hr | bool | No | Enable high-resolution fix (two-pass generation); default False |
| denoising_strength | float | No | Denoising strength for hires fix second pass; default 0.75 |
| hr_scale | float | No | Upscale factor for hires fix; default 2.0 |
| hr_upscaler | str | No | Name of the upscaler to use for hires fix |
| hr_second_pass_steps | int | No | Number of steps for hires fix second pass; 0 uses first pass steps; default 0 |
| hr_checkpoint_name | str | No | Optional different checkpoint for the hires pass |
| hr_sampler_name | str | No | Optional different sampler for the hires pass |
| hr_prompt | str | No | Optional different prompt for the hires pass; default (uses main prompt) |
| hr_negative_prompt | str | No | Optional different negative prompt for the hires pass; default (uses main negative) |
Outputs
| Name | Type | Description |
|---|---|---|
| sample() return | torch.Tensor or DecodedSamples | Latent tensor of shape (B, 4, H/8, W/8) when hires fix is disabled, or DecodedSamples (list of decoded image tensors) when hires fix is enabled |
Usage Examples
Basic Usage
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
import modules.shared as shared
p = StableDiffusionProcessingTxt2Img(
sd_model=shared.sd_model,
prompt="a beautiful sunset over the ocean, oil painting",
negative_prompt="blurry, low quality",
seed=42,
width=512,
height=512,
steps=30,
cfg_scale=7.5,
sampler_name="DPM++ 2M",
scheduler="Karras",
)
processed = process_images(p)
# processed.images contains the generated PIL images
With Hires Fix
p = StableDiffusionProcessingTxt2Img(
sd_model=shared.sd_model,
prompt="detailed portrait of a knight in shining armor",
negative_prompt="deformed, ugly",
seed=123,
width=512,
height=768,
steps=25,
cfg_scale=7.0,
sampler_name="Euler a",
enable_hr=True,
hr_scale=2.0,
hr_upscaler="Latent",
hr_second_pass_steps=15,
denoising_strength=0.55,
)
processed = process_images(p)
# Final images are 1024x1536 after hires fix
Related Pages
Implements Principle
Requires Environment
- Environment:AUTOMATIC1111_Stable_diffusion_webui_Python_And_PyTorch_Runtime
- Environment:AUTOMATIC1111_Stable_diffusion_webui_GPU_Compute_Backend