Python API client for AUTOMATIC1111/stable-diffusion-webui enabling programmatic Stable Diffusion image generation
Core image generation functionality providing text-to-image and image-to-image capabilities with extensive parameter control. These functions form the foundation of WebUIApi's Stable Diffusion integration.
Generate images from text prompts using Stable Diffusion models. Supports extensive customization including sampling methods, guidance scale, resolution, and batch generation.
def txt2img(
prompt: str,
negative_prompt: str = "",
width: int = 512,
height: int = 512,
steps: int = 20,
sampler_name: str = None,
scheduler: str = None,
cfg_scale: float = 7.0,
seed: int = -1,
batch_size: int = 1,
batch_count: int = 1,
restore_faces: bool = False,
tiling: bool = False,
enable_hr: bool = False,
hr_scale: float = 2.0,
hr_upscaler: str = "Latent",
hr_second_pass_steps: int = 0,
hr_resize_x: int = 0,
hr_resize_y: int = 0,
denoising_strength: float = 0.7,
override_settings: Dict = None,
override_settings_restore_afterwards: bool = True,
script_name: str = None,
script_args: List = None,
send_images: bool = True,
save_images: bool = False,
alwayson_scripts: Dict = None,
controlnet_units: List[ControlNetUnit] = None,
**kwargs
) -> WebUIApiResult:
"""
Generate images from text prompts.
Parameters:
- prompt: Text description of desired image
- negative_prompt: Text describing what to avoid
- width, height: Image dimensions (default 512x512)
- steps: Number of sampling steps (default 20)
- sampler_name: Sampling method (uses default if None)
- scheduler: Scheduler type (uses default if None)
- cfg_scale: Classifier-free guidance scale (default 7.0)
- seed: Random seed (-1 for random)
- batch_size: Number of images per batch
- batch_count: Number of batches to generate
- restore_faces: Apply face restoration
- tiling: Enable seamless tiling
- enable_hr: Enable high-res fix
- hr_scale: High-res upscaling factor
- hr_upscaler: High-res upscaling method
- hr_second_pass_steps: Steps for high-res pass
- hr_resize_x, hr_resize_y: High-res target dimensions
- denoising_strength: Denoising strength for high-res pass
- override_settings: Temporary setting overrides
- override_settings_restore_afterwards: Restore settings after generation
- script_name: Script to execute during generation
- script_args: Arguments for the script
- send_images: Return images in response
- save_images: Save images to WebUI output folder
- alwayson_scripts: Always-on script configurations
- controlnet_units: ControlNet conditioning units
Returns:
WebUIApiResult containing generated images and metadata
"""Usage Example:
import webuiapi
api = webuiapi.WebUIApi()
# Basic text-to-image
result = api.txt2img(
prompt="a serene mountain landscape, oil painting style",
negative_prompt="blurry, low quality, deformed",
width=768,
height=512,
steps=30,
cfg_scale=8.0,
seed=42
)
# High-resolution generation with face restoration
result = api.txt2img(
prompt="portrait of a person, detailed face, professional photography",
width=512,
height=768,
enable_hr=True,
hr_scale=2.0,
hr_upscaler="R-ESRGAN 4x+",
restore_faces=True,
steps=25
)
# Batch generation
result = api.txt2img(
prompt="abstract art, colorful geometric shapes",
batch_size=4,
batch_count=2, # Generates 8 images total
seed=-1 # Random seeds for each image
)Transform existing images using text prompts while preserving aspects of the original composition. Supports various conditioning strengths and resize modes.
def img2img(
prompt: str,
images: List[Image.Image],
negative_prompt: str = "",
width: int = 512,
height: int = 512,
steps: int = 20,
sampler_name: str = None,
scheduler: str = None,
cfg_scale: float = 7.0,
denoising_strength: float = 0.75,
seed: int = -1,
batch_size: int = 1,
batch_count: int = 1,
resize_mode: int = 0,
image_cfg_scale: float = None,
mask: Image.Image = None,
mask_blur: int = 0,
inpainting_fill: int = 1,
inpaint_full_res: bool = True,
inpaint_full_res_padding: int = 0,
inpainting_mask_invert: int = 0,
initial_noise_multiplier: float = None,
restore_faces: bool = False,
tiling: bool = False,
override_settings: Dict = None,
override_settings_restore_afterwards: bool = True,
script_name: str = None,
script_args: List = None,
send_images: bool = True,
save_images: bool = False,
alwayson_scripts: Dict = None,
controlnet_units: List[ControlNetUnit] = None,
**kwargs
) -> WebUIApiResult:
"""
Generate images from existing images with text prompts.
Parameters:
- prompt: Text description for transformation
- images: List of input images to transform
- negative_prompt: Text describing what to avoid
- width, height: Output image dimensions
- steps: Number of sampling steps
- sampler_name: Sampling method
- scheduler: Scheduler type
- cfg_scale: Classifier-free guidance scale
- denoising_strength: How much to change the image (0.0-1.0)
- seed: Random seed (-1 for random)
- batch_size: Number of images per batch
- batch_count: Number of batches
- resize_mode: How to handle size differences (0=Just resize, 1=Crop and resize, 2=Resize and fill, 3=Just resize (latent upscale))
- image_cfg_scale: CFG scale for image conditioning
- mask: Mask for inpainting (optional)
- mask_blur: Mask blur radius
- inpainting_fill: Fill mode for masked area (0=fill, 1=original, 2=latent noise, 3=latent nothing)
- inpaint_full_res: Inpaint at full resolution
- inpaint_full_res_padding: Padding for full resolution inpainting
- inpainting_mask_invert: Invert mask (0=inpaint masked, 1=inpaint not masked)
- initial_noise_multiplier: Noise multiplier for initial latent
- restore_faces: Apply face restoration
- tiling: Enable seamless tiling
- override_settings: Temporary setting overrides
- override_settings_restore_afterwards: Restore settings after
- script_name: Script to execute
- script_args: Script arguments
- send_images: Return images in response
- save_images: Save images to output folder
- alwayson_scripts: Always-on script configurations
- controlnet_units: ControlNet conditioning units
Returns:
WebUIApiResult containing transformed images and metadata
"""Usage Example:
from PIL import Image
import webuiapi
api = webuiapi.WebUIApi()
# Load input image
init_image = Image.open("photo.jpg")
# Style transfer
result = api.img2img(
prompt="oil painting, impressionist style, vibrant colors",
images=[init_image],
denoising_strength=0.6,
width=768,
height=768,
resize_mode=1 # Crop and resize
)
# Inpainting with mask
mask_image = Image.open("mask.png") # White areas will be inpainted
result = api.img2img(
prompt="beautiful garden with flowers",
images=[init_image],
mask=mask_image,
denoising_strength=1.0,
inpainting_fill=1, # Use original image for context
inpaint_full_res=True
)
# Multiple variations
result = api.img2img(
prompt="same subject, different lighting",
images=[init_image],
denoising_strength=0.4,
batch_size=3,
seed=-1 # Different seed for each variation
)class WebUIApiResult:
"""Result container for image generation operations."""
images: List[Image.Image] # Generated images as PIL Image objects
parameters: Dict # Parameters used for generation
info: Dict # Generation info and metadata
json: Dict # Raw API response
@property
def image(self) -> Image.Image:
"""First generated image (convenience property)."""Install with Tessl CLI
npx tessl i tessl/pypi-webuiapi