Implementation:AUTOMATIC1111 Stable diffusion webui Img2img
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, Image Editing, Inpainting, Diffusion Models |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for dispatching image-to-image generation requests across multiple input modes (standard, sketch, inpaint, batch) provided by the AUTOMATIC1111 stable-diffusion-webui repository.
Description
The img2img function is the top-level entry point for all image-to-image generation in the WebUI. It receives the Gradio UI state, interprets the integer mode parameter to select the appropriate image source and mask, constructs a StableDiffusionProcessingImg2Img object, and delegates to either batch processing or single-image generation.
The mode dispatch logic maps each integer to a specific image/mask extraction strategy:
- Mode 0 (img2img): Uses
init_imgdirectly, no mask. - Mode 1 (sketch): Uses the
sketchcanvas input, no mask. - Mode 2 (inpaint): Extracts image and mask from
init_img_with_maskdict, converts mask to binary viacreate_binary_mask(). - Mode 3 (inpaint sketch): Computes mask by detecting pixel differences between the edited
inpaint_color_sketchand the originalinpaint_color_sketch_orig. Applies brightness adjustment withmask_alphaand Gaussian blur compositing. - Mode 4 (inpaint upload mask): Uses separately uploaded
init_img_inpaintandinit_mask_inpaint. - Mode 5 (batch): Delegates to
process_batch()for directory-based or upload-based batch processing.
After mode dispatch, the function validates denoising strength is within [0.0, 1.0], optionally scales dimensions by scale_by, creates the processing object, and invokes the generation pipeline.
Usage
This function is called by the Gradio UI when the user clicks "Generate" on the img2img tab. It is also accessible through the WebUI API for programmatic image-to-image workflows.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/img2img.py - Lines: 152-253
Signature
def img2img(
id_task: str,
request: gr.Request,
mode: int,
prompt: str,
negative_prompt: str,
prompt_styles,
init_img,
sketch,
init_img_with_mask,
inpaint_color_sketch,
inpaint_color_sketch_orig,
init_img_inpaint,
init_mask_inpaint,
mask_blur: int,
mask_alpha: float,
inpainting_fill: int,
n_iter: int,
batch_size: int,
cfg_scale: float,
image_cfg_scale: float,
denoising_strength: float,
selected_scale_tab: int,
height: int,
width: int,
scale_by: float,
resize_mode: int,
inpaint_full_res: bool,
inpaint_full_res_padding: int,
inpainting_mask_invert: int,
img2img_batch_input_dir: str,
img2img_batch_output_dir: str,
img2img_batch_inpaint_mask_dir: str,
override_settings_texts,
img2img_batch_use_png_info: bool,
img2img_batch_png_info_props: list,
img2img_batch_png_info_dir: str,
img2img_batch_source_type: str,
img2img_batch_upload: list,
*args
) -> tuple[list, str, str, str]:
Import
from modules.img2img import img2img
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| id_task | str | Yes | Unique task identifier for progress tracking |
| request | gr.Request | Yes | Gradio request object containing user info |
| mode | int | Yes | Input mode: 0=img2img, 1=sketch, 2=inpaint, 3=inpaint sketch, 4=inpaint upload mask, 5=batch |
| prompt | str | Yes | Text prompt describing the desired output |
| negative_prompt | str | Yes | Text prompt describing undesired content |
| prompt_styles | list | No | Named style presets to apply |
| init_img | PIL.Image | No | Source image for mode 0 |
| sketch | PIL.Image | No | Sketch canvas image for mode 1 |
| init_img_with_mask | dict | No | Dict with "image" and "mask" keys for mode 2 |
| inpaint_color_sketch | PIL.Image | No | User-edited image for mode 3 |
| inpaint_color_sketch_orig | PIL.Image | No | Original unedited image for mode 3 diff computation |
| init_img_inpaint | PIL.Image | No | Source image for mode 4 |
| init_mask_inpaint | PIL.Image | No | Uploaded mask for mode 4 |
| mask_blur | int | Yes | Gaussian blur radius applied to inpainting mask |
| mask_alpha | float | Yes | Mask transparency for inpaint sketch mode (0-100) |
| inpainting_fill | int | Yes | Fill method for masked area: 0=fill, 1=original, 2=latent noise, 3=latent nothing |
| denoising_strength | float | Yes | Controls noise level added to input (0.0-1.0) |
| resize_mode | int | Yes | How to resize input: 0=just resize, 1=crop and resize, 2=resize and fill, 3=latent upscale |
| cfg_scale | float | Yes | Classifier-free guidance scale |
| width | int | Yes | Target output width in pixels |
| height | int | Yes | Target output height in pixels |
Outputs
| Name | Type | Description |
|---|---|---|
| images | list[PIL.Image] | Generated output images |
| generation_info_js | str | JSON string containing generation metadata |
| info_html | str | HTML-formatted generation info for UI display |
| comments_html | str | HTML-formatted comments and warnings |
Usage Examples
Basic Usage
from modules.img2img import img2img
# Standard img2img call (mode 0)
images, info_js, info_html, comments = img2img(
id_task="task_001",
request=gradio_request,
mode=0, # standard img2img
prompt="a beautiful landscape painting",
negative_prompt="blurry, low quality",
prompt_styles=[],
init_img=source_image, # PIL.Image
sketch=None,
init_img_with_mask=None,
inpaint_color_sketch=None,
inpaint_color_sketch_orig=None,
init_img_inpaint=None,
init_mask_inpaint=None,
mask_blur=4,
mask_alpha=0.0,
inpainting_fill=0,
n_iter=1,
batch_size=1,
cfg_scale=7.0,
image_cfg_scale=1.5,
denoising_strength=0.75,
selected_scale_tab=0,
height=512,
width=512,
scale_by=1.0,
resize_mode=0,
inpaint_full_res=False,
inpaint_full_res_padding=0,
inpainting_mask_invert=0,
img2img_batch_input_dir="",
img2img_batch_output_dir="",
img2img_batch_inpaint_mask_dir="",
override_settings_texts=[],
img2img_batch_use_png_info=False,
img2img_batch_png_info_props=[],
img2img_batch_png_info_dir="",
img2img_batch_source_type="from dir",
img2img_batch_upload=[],
)