0
# Image Generation
1
2
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.
3
4
## Capabilities
5
6
### Text-to-Image Generation
7
8
Generate images from text prompts using Stable Diffusion models. Supports extensive customization including sampling methods, guidance scale, resolution, and batch generation.
9
10
```python { .api }
11
def txt2img(
12
prompt: str,
13
negative_prompt: str = "",
14
width: int = 512,
15
height: int = 512,
16
steps: int = 20,
17
sampler_name: str = None,
18
scheduler: str = None,
19
cfg_scale: float = 7.0,
20
seed: int = -1,
21
batch_size: int = 1,
22
batch_count: int = 1,
23
restore_faces: bool = False,
24
tiling: bool = False,
25
enable_hr: bool = False,
26
hr_scale: float = 2.0,
27
hr_upscaler: str = "Latent",
28
hr_second_pass_steps: int = 0,
29
hr_resize_x: int = 0,
30
hr_resize_y: int = 0,
31
denoising_strength: float = 0.7,
32
override_settings: Dict = None,
33
override_settings_restore_afterwards: bool = True,
34
script_name: str = None,
35
script_args: List = None,
36
send_images: bool = True,
37
save_images: bool = False,
38
alwayson_scripts: Dict = None,
39
controlnet_units: List[ControlNetUnit] = None,
40
**kwargs
41
) -> WebUIApiResult:
42
"""
43
Generate images from text prompts.
44
45
Parameters:
46
- prompt: Text description of desired image
47
- negative_prompt: Text describing what to avoid
48
- width, height: Image dimensions (default 512x512)
49
- steps: Number of sampling steps (default 20)
50
- sampler_name: Sampling method (uses default if None)
51
- scheduler: Scheduler type (uses default if None)
52
- cfg_scale: Classifier-free guidance scale (default 7.0)
53
- seed: Random seed (-1 for random)
54
- batch_size: Number of images per batch
55
- batch_count: Number of batches to generate
56
- restore_faces: Apply face restoration
57
- tiling: Enable seamless tiling
58
- enable_hr: Enable high-res fix
59
- hr_scale: High-res upscaling factor
60
- hr_upscaler: High-res upscaling method
61
- hr_second_pass_steps: Steps for high-res pass
62
- hr_resize_x, hr_resize_y: High-res target dimensions
63
- denoising_strength: Denoising strength for high-res pass
64
- override_settings: Temporary setting overrides
65
- override_settings_restore_afterwards: Restore settings after generation
66
- script_name: Script to execute during generation
67
- script_args: Arguments for the script
68
- send_images: Return images in response
69
- save_images: Save images to WebUI output folder
70
- alwayson_scripts: Always-on script configurations
71
- controlnet_units: ControlNet conditioning units
72
73
Returns:
74
WebUIApiResult containing generated images and metadata
75
"""
76
```
77
78
**Usage Example:**
79
80
```python
81
import webuiapi
82
83
api = webuiapi.WebUIApi()
84
85
# Basic text-to-image
86
result = api.txt2img(
87
prompt="a serene mountain landscape, oil painting style",
88
negative_prompt="blurry, low quality, deformed",
89
width=768,
90
height=512,
91
steps=30,
92
cfg_scale=8.0,
93
seed=42
94
)
95
96
# High-resolution generation with face restoration
97
result = api.txt2img(
98
prompt="portrait of a person, detailed face, professional photography",
99
width=512,
100
height=768,
101
enable_hr=True,
102
hr_scale=2.0,
103
hr_upscaler="R-ESRGAN 4x+",
104
restore_faces=True,
105
steps=25
106
)
107
108
# Batch generation
109
result = api.txt2img(
110
prompt="abstract art, colorful geometric shapes",
111
batch_size=4,
112
batch_count=2, # Generates 8 images total
113
seed=-1 # Random seeds for each image
114
)
115
```
116
117
### Image-to-Image Generation
118
119
Transform existing images using text prompts while preserving aspects of the original composition. Supports various conditioning strengths and resize modes.
120
121
```python { .api }
122
def img2img(
123
prompt: str,
124
images: List[Image.Image],
125
negative_prompt: str = "",
126
width: int = 512,
127
height: int = 512,
128
steps: int = 20,
129
sampler_name: str = None,
130
scheduler: str = None,
131
cfg_scale: float = 7.0,
132
denoising_strength: float = 0.75,
133
seed: int = -1,
134
batch_size: int = 1,
135
batch_count: int = 1,
136
resize_mode: int = 0,
137
image_cfg_scale: float = None,
138
mask: Image.Image = None,
139
mask_blur: int = 0,
140
inpainting_fill: int = 1,
141
inpaint_full_res: bool = True,
142
inpaint_full_res_padding: int = 0,
143
inpainting_mask_invert: int = 0,
144
initial_noise_multiplier: float = None,
145
restore_faces: bool = False,
146
tiling: bool = False,
147
override_settings: Dict = None,
148
override_settings_restore_afterwards: bool = True,
149
script_name: str = None,
150
script_args: List = None,
151
send_images: bool = True,
152
save_images: bool = False,
153
alwayson_scripts: Dict = None,
154
controlnet_units: List[ControlNetUnit] = None,
155
**kwargs
156
) -> WebUIApiResult:
157
"""
158
Generate images from existing images with text prompts.
159
160
Parameters:
161
- prompt: Text description for transformation
162
- images: List of input images to transform
163
- negative_prompt: Text describing what to avoid
164
- width, height: Output image dimensions
165
- steps: Number of sampling steps
166
- sampler_name: Sampling method
167
- scheduler: Scheduler type
168
- cfg_scale: Classifier-free guidance scale
169
- denoising_strength: How much to change the image (0.0-1.0)
170
- seed: Random seed (-1 for random)
171
- batch_size: Number of images per batch
172
- batch_count: Number of batches
173
- resize_mode: How to handle size differences (0=Just resize, 1=Crop and resize, 2=Resize and fill, 3=Just resize (latent upscale))
174
- image_cfg_scale: CFG scale for image conditioning
175
- mask: Mask for inpainting (optional)
176
- mask_blur: Mask blur radius
177
- inpainting_fill: Fill mode for masked area (0=fill, 1=original, 2=latent noise, 3=latent nothing)
178
- inpaint_full_res: Inpaint at full resolution
179
- inpaint_full_res_padding: Padding for full resolution inpainting
180
- inpainting_mask_invert: Invert mask (0=inpaint masked, 1=inpaint not masked)
181
- initial_noise_multiplier: Noise multiplier for initial latent
182
- restore_faces: Apply face restoration
183
- tiling: Enable seamless tiling
184
- override_settings: Temporary setting overrides
185
- override_settings_restore_afterwards: Restore settings after
186
- script_name: Script to execute
187
- script_args: Script arguments
188
- send_images: Return images in response
189
- save_images: Save images to output folder
190
- alwayson_scripts: Always-on script configurations
191
- controlnet_units: ControlNet conditioning units
192
193
Returns:
194
WebUIApiResult containing transformed images and metadata
195
"""
196
```
197
198
**Usage Example:**
199
200
```python
201
from PIL import Image
202
import webuiapi
203
204
api = webuiapi.WebUIApi()
205
206
# Load input image
207
init_image = Image.open("photo.jpg")
208
209
# Style transfer
210
result = api.img2img(
211
prompt="oil painting, impressionist style, vibrant colors",
212
images=[init_image],
213
denoising_strength=0.6,
214
width=768,
215
height=768,
216
resize_mode=1 # Crop and resize
217
)
218
219
# Inpainting with mask
220
mask_image = Image.open("mask.png") # White areas will be inpainted
221
222
result = api.img2img(
223
prompt="beautiful garden with flowers",
224
images=[init_image],
225
mask=mask_image,
226
denoising_strength=1.0,
227
inpainting_fill=1, # Use original image for context
228
inpaint_full_res=True
229
)
230
231
# Multiple variations
232
result = api.img2img(
233
prompt="same subject, different lighting",
234
images=[init_image],
235
denoising_strength=0.4,
236
batch_size=3,
237
seed=-1 # Different seed for each variation
238
)
239
```
240
241
## Types
242
243
```python { .api }
244
class WebUIApiResult:
245
"""Result container for image generation operations."""
246
images: List[Image.Image] # Generated images as PIL Image objects
247
parameters: Dict # Parameters used for generation
248
info: Dict # Generation info and metadata
249
json: Dict # Raw API response
250
251
@property
252
def image(self) -> Image.Image:
253
"""First generated image (convenience property)."""
254
```