Python API client for AUTOMATIC1111/stable-diffusion-webui enabling programmatic Stable Diffusion image generation
npx @tessl/cli install tessl/pypi-webuiapi@0.9.0A comprehensive Python API client for AUTOMATIC1111/stable-diffusion-webui that enables programmatic image generation using Stable Diffusion models. WebUIApi provides complete access to txt2img, img2img, upscaling, model management, and extensive extension support including ControlNet, ADetailer, AnimateDiff, and face swapping capabilities.
pip install webuiapiimport webuiapi
# Create API client
api = webuiapi.WebUIApi()Specific imports:
from webuiapi import (
WebUIApi,
WebUIApiResult,
ControlNetUnit,
ADetailer,
Upscaler,
b64_img
)import webuiapi
# Create API client with default settings
api = webuiapi.WebUIApi()
# Or with custom configuration
api = webuiapi.WebUIApi(
host='127.0.0.1',
port=7860,
sampler='Euler a',
steps=20
)
# Simple text-to-image generation
result = api.txt2img(
prompt="cute cat, digital art",
negative_prompt="blurry, low quality",
width=512,
height=512
)
# Save the generated image
result.image.save("generated_cat.png")
# Image-to-image generation
from PIL import Image
init_image = Image.open("input.jpg")
result = api.img2img(
prompt="oil painting style",
images=[init_image],
denoising_strength=0.7
)
result.image.save("styled_image.png")WebUIApi follows a modular design centered around the main API client:
The API maintains connection state, handles authentication, and provides both synchronous operations and extension integration through a unified interface.
Core text-to-image and image-to-image generation functionality with extensive parameter control including sampling methods, steps, guidance scale, and batch processing.
def txt2img(
prompt: str,
negative_prompt: str = "",
width: int = 512,
height: int = 512,
steps: int = 20,
sampler_name: str = None,
cfg_scale: float = 7.0,
seed: int = -1,
batch_size: int = 1,
**kwargs
) -> WebUIApiResult: ...
def img2img(
prompt: str,
images: List[Image.Image],
negative_prompt: str = "",
denoising_strength: float = 0.75,
width: int = 512,
height: int = 512,
**kwargs
) -> WebUIApiResult: ...Complete model lifecycle management including loading, switching, and querying available models, samplers, schedulers, and other resources.
def get_sd_models() -> List[Dict]: ...
def get_samplers() -> List[Dict]: ...
def get_schedulers() -> List[Dict]: ...
def util_set_model(name: str) -> None: ...
def util_get_current_model() -> str: ...Advanced image processing operations including upscaling, interrogation, metadata extraction, and batch processing capabilities.
def extra_single_image(
image: Image.Image,
upscaler_1: str = "None",
upscaling_resize: float = 2.0,
**kwargs
) -> WebUIApiResult: ...
def interrogate(
image: Image.Image,
model: str = "clip"
) -> str: ...
def png_info(image: Image.Image) -> Dict: ...Precise image generation control using ControlNet with support for depth, canny, pose, and other conditioning methods.
class ControlNetUnit:
def __init__(
self,
image: Image.Image = None,
module: str = "none",
model: str = "None",
weight: float = 1.0,
**kwargs
): ...
def controlnet_detect(
controlnet_module: str,
controlnet_input_images: List[str],
**kwargs
) -> Dict: ...Integration with popular WebUI extensions including ADetailer for face enhancement, AnimateDiff for video generation, and face swapping capabilities.
class ADetailer:
def __init__(
self,
ad_model: str = "face_yolov8n.pt",
ad_confidence: float = 0.3,
ad_denoising_strength: float = 0.4,
**kwargs
): ...
class AnimateDiff:
def __init__(
self,
model: str = "mm_sd_v14.ckpt",
fps: int = 8,
video_length: int = 16,
**kwargs
): ...WebUI configuration management, option retrieval and modification, and system status monitoring.
def get_options() -> Dict: ...
def set_options(options: Dict) -> None: ...
def get_progress() -> Dict: ...
def interrupt() -> None: ...Helper functions for image encoding and common operations.
def b64_img(image: Image.Image) -> str:
"""Convert PIL Image to base64 with data URL header."""
def raw_b64_img(image: Image.Image) -> str:
"""Convert PIL Image to raw base64 (no header)."""Specialized interfaces for extension functionality and image analysis.
class ModelKeywordInterface:
def get_keywords(self, model_name: str) -> ModelKeywordResult: ...
class SegmentAnythingInterface:
def sam_predict(self, **kwargs) -> SegmentAnythingSamResult: ...
def dino_predict(self, **kwargs) -> SegmentAnythingGinoResult: ...
class TaggerInterface:
def tagger_interrogate(self, **kwargs) -> Dict: ...__version__: str = "0.9.17" # Package version constant
class WebUIApiResult:
"""Container for API response data."""
images: List[Image.Image] # Generated images
parameters: Dict # Generation parameters used
info: Dict # Additional generation info
json: Dict # Raw JSON response
@property
def image(self) -> Image.Image:
"""First generated image (convenience property)."""
class Upscaler(str, Enum):
"""Available upscaling algorithms."""
none = "None"
Lanczos = "Lanczos"
ESRGAN_4x = "R-ESRGAN 4x+"
# ... additional upscaler options
class HiResUpscaler(str, Enum):
"""High-resolution upscaling methods."""
none = "None"
Latent = "Latent"
LatentAntialiased = "Latent (antialiased)"
# ... additional hi-res options
class ModelKeywordResult:
"""Result from model keyword lookup."""
keywords: List[str] # Associated keywords
model: str # Model name
oldhash: str # Model hash
match_source: str # Source of keyword match
# SegmentAnything Result Classes
class SegmentAnythingSamResult:
"""Result from SAM prediction operations."""
class SegmentAnythingGinoResult:
"""Result from DINO prediction operations."""
class SegmentAnythingDilationResult:
"""Result from mask dilation operations."""
class SegmentAnythingControlNetSegRandomResult:
"""Result from ControlNet segmentation (random mode)."""
class SegmentAnythingControlNetSegNotRandomResult:
"""Result from ControlNet segmentation (non-random mode)."""
class SegmentAnythingSemanticSegWithCatIdResult:
"""Result from semantic segmentation with category ID."""