Create images

import requests

url = "https://playground.com/api/models/external/v1"

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_API_KEY"
}
data = {
    "prompt": "a frog",
    "filter_model": "Playground_v2.5",
    "scheduler": "DPMPP_2M_K",
    "steps": 30
}

response = requests.post(url, headers=headers, json=data)

For full details on what options are available, check out the API Reference.

This will return the image as base64. You'll have to parse it:

import io
import base64
from PIL import Image

def base64_to_image(base64_string):
    image_data = base64.b64decode(base64_string)
    return Image.open(io.BytesIO(image_data))

data = response.json()

base64_to_image(data["images"][0]).save("result.png")

Use an initial image as inspiration

Image to image utilizes an initial image to create an image that is structurally similar. You can use img2img in Playground by supplying the init_image parameter.

def get_base64_from_file(filename):
    with open(filename, "rb") as f:
        return base64.b64encode(f.read()).decode()

# same headers as above!
      
data = {
    "prompt": "a toad",
    "init_image": get_base64_from_file("img2img_init.png"),
    "strength": 0.5
}

res = requests.post("https://playgroundai.com/api/models/external/v1", json=body, headers=headers)
data = res.json()

base64_to_image(data["images"][0]).save("./img2img_result.png")

Playground v3 (beta)

With recommended params:

import requests
resp = requests.post('https://playground.com/api/models/external/v1', json={
    "prompt": "A woman with the word 'shannon' floating above her head",
    "filter_model": "Playground_v3",
    "steps": 50, # for quicker results, use 30
    "guidance_scale": 6,
    # "response_type": "url", # if you want to have the response as a url. default is b64
    "width": 1024,
    "height": 1024
}, headers={
    "Authorization": "Bearer " + API_KEY,
})
print(resp)
d = resp.json()

For Playground v3, width and height must be divisible by 32.