Implementation:AUTOMATIC1111 Stable diffusion webui Run postprocessing
| Knowledge Sources | |
|---|---|
| Domains | Image Processing, Pipeline Design, Batch Processing |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for dispatching image postprocessing across single-image, batch-upload, and batch-directory modes provided by stable-diffusion-webui.
Description
run_postprocessing is the main entry point for all image postprocessing in the AUTOMATIC1111 Stable Diffusion WebUI. It accepts images from three distinct input modes (single image, batch upload, batch directory), normalizes them into a common processing loop, runs the full postprocessing script pipeline (upscaling, face restoration, custom scripts) on each image, and saves the results with appropriate metadata.
The function uses an inner generator get_images() to yield (image_or_path, filename) pairs from whichever input mode is selected. Directory mode supports lazy loading where file paths are yielded first and only resolved to PIL Image objects in the main loop. After processing, each image and its extra outputs are saved with embedded PNG info including postprocessing parameters. The function also supports caption file management with configurable actions (prepend, append, keep, or replace) for existing captions.
Usage
Use run_postprocessing when you need to apply the full extras/postprocessing pipeline to images. It is called directly by the Extras tab in the WebUI and by the API endpoint for postprocessing operations.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/postprocessing.py - Lines: 9-129
Signature
def run_postprocessing(
extras_mode,
image,
image_folder,
input_dir,
output_dir,
show_extras_results,
*args,
save_output: bool = True
) -> tuple[list, str, str]:
Import
from modules.postprocessing import run_postprocessing
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| extras_mode | int | Yes | Input mode selector: 0 = single image, 1 = batch upload, 2 = batch directory |
| image | PIL.Image | No | The single input image (required when extras_mode == 0) |
| image_folder | list | No | List of uploaded image files or PIL.Image objects (required when extras_mode == 1) |
| input_dir | str | No | Filesystem path to input directory (required when extras_mode == 2) |
| output_dir | str | No | Filesystem path to output directory; used when extras_mode == 2 and non-empty, otherwise falls back to configured output path |
| show_extras_results | bool | Yes | Whether to include processed images in the returned output list when in directory mode (extras_mode == 2) |
| *args | tuple | Yes | Arguments passed through to the postprocessing script pipeline (ScriptPostprocessingRunner.run) |
| save_output | bool | No | Whether to save processed images to disk (default: True) |
Outputs
| Name | Type | Description |
|---|---|---|
| outputs | list[PIL.Image] | List of processed images (may be empty in directory mode if show_extras_results is False) |
| infotext | str | HTML-formatted string containing postprocessing parameters for the last processed image |
| empty_string | str | Always returns an empty string (reserved for UI compatibility) |
Usage Examples
Basic Usage
from modules.postprocessing import run_postprocessing
from PIL import Image
# Single image mode
img = Image.open("input.png")
results, info_html, _ = run_postprocessing(
extras_mode=0,
image=img,
image_folder=None,
input_dir="",
output_dir="",
show_extras_results=True,
*script_args,
save_output=True,
)
Batch Directory Mode
# Process all images in a directory
results, info_html, _ = run_postprocessing(
extras_mode=2,
image=None,
image_folder=None,
input_dir="/path/to/input/images",
output_dir="/path/to/output",
show_extras_results=False,
*script_args,
save_output=True,
)