CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-together

Python client for Together's Cloud Platform providing comprehensive AI model APIs

Overview
Eval results
Files

images.mddocs/

Image Generation

AI-powered image synthesis from text prompts with support for multiple models, resolutions, generation parameters, and output formats. Create high-quality images for creative projects, design workflows, and visual content generation.

Capabilities

Image Generation

Generate images from text descriptions using state-of-the-art diffusion models.

def generate(
    prompt: str,
    model: str,
    n: int = 1,
    steps: Optional[int] = None,
    seed: Optional[int] = None,
    height: Optional[int] = None,
    width: Optional[int] = None,
    **kwargs
) -> ImageResponse:
    """
    Generate images from text prompts.

    Args:
        prompt: Text description of desired image
        model: Image generation model identifier
        n: Number of images to generate (1-4)
        steps: Number of inference steps
        seed: Random seed for reproducible results
        height: Image height in pixels
        width: Image width in pixels

    Returns:
        ImageResponse with generated image data
    """

Async Image Generation

Asynchronous image generation for concurrent processing.

async def generate(
    prompt: str,
    model: str,
    **kwargs
) -> ImageResponse:
    """
    Asynchronously generate images from prompts.

    Returns:
        ImageResponse with generated image data
    """

Usage Examples

Basic Image Generation

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

client = Together()

response = client.images.generate(
    prompt="A serene mountain landscape at sunset with reflection in a lake",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    n=1,
    steps=20,
    width=1024,
    height=768
)

# Get base64 encoded image data
image_data = response.data[0].b64_json

# Decode and save image
image_bytes = base64.b64decode(image_data)
image = Image.open(io.BytesIO(image_bytes))
image.save("generated_landscape.png")
print("Image saved successfully!")

Multiple Image Variations

response = client.images.generate(
    prompt="Abstract geometric patterns in vibrant colors",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    n=4,  # Generate 4 variations
    steps=25,
    width=512,
    height=512,
    seed=42  # For reproducible results
)

# Save all generated images
for i, image_data in enumerate(response.data):
    image_bytes = base64.b64decode(image_data.b64_json)
    image = Image.open(io.BytesIO(image_bytes))
    image.save(f"abstract_art_{i+1}.png")

print(f"Generated and saved {len(response.data)} images")

High-Quality Portrait Generation

response = client.images.generate(
    prompt="Professional headshot of a business executive, studio lighting, high resolution, photorealistic",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    n=1,
    steps=30,
    width=768,
    height=1024,
    seed=123
)

image_data = response.data[0].b64_json
image_bytes = base64.b64decode(image_data)
image = Image.open(io.BytesIO(image_bytes))
image.save("professional_portrait.jpg", "JPEG", quality=95)

Artistic Style Generation

artistic_prompts = [
    "Van Gogh style painting of a starry night over a cityscape",
    "Minimalist line drawing of a cat, black ink on white paper",
    "Watercolor painting of cherry blossoms in spring",
    "Digital art cyberpunk city with neon lights"
]

for i, prompt in enumerate(artistic_prompts):
    response = client.images.generate(
        prompt=prompt,
        model="stabilityai/stable-diffusion-xl-base-1.0",
        n=1,
        steps=25,
        width=768,
        height=768
    )
    
    image_bytes = base64.b64decode(response.data[0].b64_json)
    image = Image.open(io.BytesIO(image_bytes))
    image.save(f"artistic_style_{i+1}.png")
    print(f"Generated: {prompt[:50]}...")

Async Batch Image Generation

import asyncio
from together import AsyncTogether

async def generate_images_async():
    client = AsyncTogether()
    
    prompts = [
        "Futuristic robot in a workshop",
        "Peaceful garden with butterflies",
        "Abstract space scene with nebula",
        "Vintage car on a country road"
    ]
    
    tasks = [
        client.images.generate(
            prompt=prompt,
            model="stabilityai/stable-diffusion-xl-base-1.0",
            n=1,
            steps=20,
            width=512,
            height=512
        )
        for prompt in prompts
    ]
    
    responses = await asyncio.gather(*tasks)
    
    for i, response in enumerate(responses):
        image_bytes = base64.b64decode(response.data[0].b64_json)
        image = Image.open(io.BytesIO(image_bytes))
        image.save(f"async_generated_{i+1}.png")
    
    print(f"Generated {len(responses)} images asynchronously")

asyncio.run(generate_images_async())

Image Processing Utility Functions

def save_generated_images(response: ImageResponse, base_filename: str = "generated"):
    """Save all images from an ImageResponse."""
    saved_files = []
    
    for i, image_data in enumerate(response.data):
        filename = f"{base_filename}_{i+1}.png" if len(response.data) > 1 else f"{base_filename}.png"
        
        image_bytes = base64.b64decode(image_data.b64_json)
        image = Image.open(io.BytesIO(image_bytes))
        image.save(filename)
        saved_files.append(filename)
    
    return saved_files

def display_image_info(response: ImageResponse):
    """Display information about generated images."""
    print(f"Generated {len(response.data)} image(s)")
    
    for i, image_data in enumerate(response.data):
        image_bytes = base64.b64decode(image_data.b64_json)
        image = Image.open(io.BytesIO(image_bytes))
        print(f"Image {i+1}: {image.size[0]}x{image.size[1]} pixels, {image.mode} mode")

# Example usage
response = client.images.generate(
    prompt="Majestic eagle soaring over mountains",
    model="stabilityai/stable-diffusion-xl-base-1.0",
    n=2,
    steps=25
)

saved_files = save_generated_images(response, "eagle_mountain")
display_image_info(response)
print(f"Saved files: {saved_files}")

Types

Request Types

class ImageRequest:
    prompt: str
    model: str
    n: int = 1
    steps: Optional[int] = None
    seed: Optional[int] = None
    height: Optional[int] = None
    width: Optional[int] = None

Response Types

class ImageResponse:
    created: int
    data: List[ImageData]

class ImageData:
    b64_json: str
    revised_prompt: Optional[str] = None

Supported Models

Popular image generation models available:

  • stabilityai/stable-diffusion-xl-base-1.0 - High-quality general purpose
  • stabilityai/stable-diffusion-2-1 - Versatile diffusion model
  • prompthero/openjourney - Artistic and creative styles
  • wavymulder/Analog-Diffusion - Analog photography aesthetic
  • 22h/vintedois-diffusion-v0-1 - Vintage and retro styles

Install with Tessl CLI

npx tessl i tessl/pypi-together

docs

audio.md

batch.md

chat-completions.md

code-interpreter.md

completions.md

embeddings.md

endpoints.md

evaluation.md

files.md

fine-tuning.md

images.md

index.md

models.md

rerank.md

tile.json