# Stable Diffusion API: `sdapi/v1/txt2img`
The `sdapi/v1/txt2img` endpoint of the Stable Diffusion API allows you to generate realistic images based on text prompts. It utilizes the Stable Diffusion algorithm to produce high-quality images that align with the given input.
## Endpoint Details
- **URL:** `http://stablediffusionalb-1002295627.us-west-2.elb.amazonaws.com/sdapi/v1/txt2img`
- **HTTP Method:** POST
- **Content Type:** JSON
## Request Payload
The request payload should be a JSON object containing the following parameters:
- `prompt` (string, required): The main text prompt describing the desired image.
- `negative_prompt` (string, optional): Additional text prompt describing elements or qualities to avoid in the generated image.
- `seed` (integer, optional): Seed value for reproducibility. Use `-1` for random seed.
- `sampler_name` (string, optional): The name of the sampling algorithm to use. Defaults to `"DPM++ SDE Karras"`.
- `steps` (integer, optional): The number of steps for diffusion. Defaults to `20`.
- `cfg_scale` (integer, optional): The scale of the diffusion model configuration. Defaults to `7`.
- `width` (integer, optional): The desired width of the generated image. Defaults to `512`.
- `height` (integer, optional): The desired height of the generated image. Defaults to `768`.
- `restore_faces` (boolean, optional): Indicates whether to restore faces in the generated image. Defaults to `True`.
- `alwayson_scripts` (object, optional): Additional scripts to run during the diffusion process. This object contains the script names as keys and their corresponding arguments as values.
- `roop` (object, required): The script name for the Roop script. Contains the following arguments:
- `args` (list, required): A list of arguments specific to the Roop script. These arguments control the behavior and characteristics of the generated image. The list format and argument values should be provided as shown in the example.
- `Base64 of Face Image` (string): The base64-encoded representation of the face image to be used for face swapping. This image should be in JPEG format.
- `Enable` (boolean): Indicates whether face swapping should be enabled or disabled.
- `Comma separated face number(s)` (string): A comma-separated list of face numbers in the image to perform face swapping. The face numbers start from 0.
- `Model` (string): The path to the face swapping model. Currently, the available model is located at "/home/ubuntu/apps/stable-diffusion-webui/models/roop/inswapper_128.onnx".
- `Restore Face` (string): The method used to restore faces in the generated image. It can take one of the following values: "None", "CodeFormer", or "GFPGAN".
- `Restore visibility` (float): The visibility of the restored face(s) in the generated image. It is a value between 0 and 1, with a step size of 0.1. A value of 1 represents full visibility.
- `Upscaler` (string): The upscaling method to enhance the generated image. It can take one of the following values: "None", "Lanczos", "Nearest", "ESRGAN_4x", "LDSR", "R-ESRGAN 4x+", "R-ESRGAN 4x+ Anime6B", "ScuNET GAN", "ScuNET PSNR", or "SwinIR 4x".
- `Upscaler scale` (float): The scale factor for the upscaling method. It is a value between 1 and 8, with a step size of 0.1. Higher values result in larger upscaled images.
- `Upscaler visibility (if scale = 1)` (float): The visibility of the upscaled image when the scale factor is 1. It is a value between 0 and 1, with a step size of 0.1. A value of 1 represents full visibility.
- `Swap in source image` (boolean): Indicates whether face swapping should be performed in the source image.
- `Swap in generated image` (boolean): Indicates whether face swapping should be performed in the generated image.
## Response
The response from the API will be a JSON object containing the following information:
- `images` (list): A list of base64-encoded strings representing the generated image(s).
## Example
```python
import base64
import io
import requests
from PIL import Image
address = 'http://stablediffusionalb-1002295627.us-west-2.elb.amazonaws.com/'
image_file = "/Users/duccm/Works/Personal/sd-webui-roop/example/elonmusk.jpeg"
im = Image.open(image_file)
img_bytes = io.BytesIO()
im.save(img_bytes, format='JPEG')
img_base64 = base64.b64encode(img_bytes.getvalue()).decode('utf-8')
args = [img_base64, True, '0', '/home/ubuntu/apps/stable-diffusion-webui/models/roop/inswapper_128.onnx', 'CodeFormer', 1, None, 1, 'None', False, True]
prompt = "Realistic image style,Vertical orientation, Man,White short hair,Shoulder-length, crying"
neg = "ng_deepnegative_v1_75t, (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, normal quality, ((monochrome)), ((grayscale)), (verybadimagenegative_v1.3:0.8), negative_hand-neg, (lamp), badhandv4"
payload = {
"prompt": prompt,
"negative_prompt": neg,
"seed": -1,
"sampler_name": "DPM++ SDE Karras",
"steps": 20,
"cfg_scale": 7,
"width": 512,
"height": 768,
"restore_faces": True,
"alwayson_scripts": {
"roop": {
"args": args
}
}
}
response = requests.post('http://stablediffusionalb-1002295627.us-west-2.elb.amazonaws.com/sdapi/v1/txt2img', json=payload)
result = response.json()
images = result['images']
image_bytes = base64.b64decode(images[0])
image_buffer = io.BytesIO(image_bytes)
image = Image.open(image_buffer)
image.save('output_image_1.jpg')
```
Please note that the example provided above assumes that the necessary libraries are installed and the required image file exists in the specified location.
Make sure to replace the appropriate values in the example code, such as the `image_file` path and the API endpoint `address`, according to your specific setup.
The resulting generated image will be saved as `output_image_1.jpg` in the current directory.