Overview

Generate images with a text prompt.

Generating an image

To query an image model, use the .images method and specify the image model you want to use.

import os
from together import Together
import base64
from PIL import Image
import io

client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))

response = client.images.generate(
    prompt="space robots",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    steps=10,
    n=1,
)
# Decode the base64 string, create image, and save it
image_data = base64.b64decode(response.data[0].b64_json)
image = Image.open(io.BytesIO(image_data))
image.show()
image.save("image.png")