Implementation:AUTOMATIC1111 Stable diffusion webui StableDiffusionProcessingImg2Img
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, Image Editing, Inpainting, Diffusion Models |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for encapsulating all parameters and orchestrating the image-to-image generation pipeline provided by the AUTOMATIC1111 stable-diffusion-webui repository.
Description
StableDiffusionProcessingImg2Img is a dataclass that extends StableDiffusionProcessing with img2img-specific parameters. It holds the source images, mask configuration, denoising strength, inpainting fill mode, and resize behavior. The class provides two key methods: init() for preprocessing the input image into latent space, and sample() for executing the noise injection and denoising loop.
The class uses Python @dataclass decoration with repr=False. Upon construction, __post_init__ moves the mask parameter to image_mask (clearing mask to None) and resolves the initial_noise_multiplier from options if not explicitly provided.
The mask_blur parameter is implemented as a property that delegates to mask_blur_x and mask_blur_y, returning None if the two values differ.
Usage
This class is instantiated by the img2img() function in modules/img2img.py after mode dispatch. It is also used by the API endpoint for programmatic img2img requests. The process_images() function in modules/processing.py calls its init() and sample() methods as part of the generation loop.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/processing.py - Lines: 1556-1793
Signature
@dataclass(repr=False)
class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
init_images: list = None
resize_mode: int = 0
denoising_strength: float = 0.75
image_cfg_scale: float = None
mask: Any = None
mask_blur_x: int = 4
mask_blur_y: int = 4
mask_blur: int = None
mask_round: bool = True
inpainting_fill: int = 0
inpaint_full_res: bool = True
inpaint_full_res_padding: int = 0
inpainting_mask_invert: int = 0
initial_noise_multiplier: float = None
latent_mask: Image = None
force_task_id: str = None
Import
from modules.processing import StableDiffusionProcessingImg2Img
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| init_images | list[PIL.Image] | Yes | Source images to condition the generation on |
| resize_mode | int | No | How to resize input: 0=just resize, 1=crop and resize, 2=resize and fill, 3=latent upscale |
| denoising_strength | float | No | Noise-to-signal ratio controlling source fidelity (0.0-1.0, default 0.75) |
| image_cfg_scale | float | No | Image classifier-free guidance scale (used with edit-conditioned models) |
| mask | Any | No | Inpainting mask image (moved to image_mask in __post_init__) |
| mask_blur_x | int | No | Horizontal Gaussian blur radius for mask (default 4) |
| mask_blur_y | int | No | Vertical Gaussian blur radius for mask (default 4) |
| mask_blur | int | No | Convenience property setting both mask_blur_x and mask_blur_y |
| mask_round | bool | No | Whether to binarize the mask (default True) |
| inpainting_fill | int | No | Masked content fill: 0=fill, 1=original, 2=latent noise, 3=latent nothing |
| inpaint_full_res | bool | No | Whether to inpaint only the masked region at full resolution (default True) |
| inpaint_full_res_padding | int | No | Padding around the masked crop region in pixels (default 0) |
| inpainting_mask_invert | int | No | Whether to invert the mask (0=no, 1=yes) |
| initial_noise_multiplier | float | No | Multiplier for initial noise amplitude (default from opts) |
| latent_mask | PIL.Image | No | Optional separate mask applied in latent space |
| force_task_id | str | No | Force a specific task ID for progress tracking |
Outputs
| Name | Type | Description |
|---|---|---|
| (instance) | StableDiffusionProcessingImg2Img | Configured processing object ready for init() and sample() calls |
Internal state set by init():
| Name | Type | Description |
|---|---|---|
| init_latent | torch.Tensor | VAE-encoded latent representation of the source image |
| nmask | torch.Tensor | Latent-space mask tensor (1.0 in regions to regenerate) |
| mask | torch.Tensor | Latent-space inverse mask tensor (1.0 in regions to preserve) |
| image_conditioning | torch.Tensor | Conditioning tensor for inpainting models |
| overlay_images | list[PIL.Image] | Original image regions for post-generation compositing |
| mask_for_overlay | PIL.Image | Pixel-space mask used for final overlay compositing |
Usage Examples
Basic Usage
from modules.processing import StableDiffusionProcessingImg2Img, process_images
import modules.shared as shared
p = StableDiffusionProcessingImg2Img(
sd_model=shared.sd_model,
outpath_samples="/output/samples",
outpath_grids="/output/grids",
prompt="a watercolor painting of a forest",
negative_prompt="blurry",
init_images=[source_pil_image],
denoising_strength=0.65,
width=512,
height=512,
resize_mode=0,
cfg_scale=7.0,
)
processed = process_images(p)
result_image = processed.images[0]
Inpainting Usage
p = StableDiffusionProcessingImg2Img(
sd_model=shared.sd_model,
prompt="a red sports car",
init_images=[source_image],
mask=mask_image, # white = region to regenerate
mask_blur=4,
inpainting_fill=1, # keep original content
inpaint_full_res=True,
inpaint_full_res_padding=32,
denoising_strength=0.8,
width=512,
height=512,
)
processed = process_images(p)