0
# Image Generation
1
2
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.
3
4
## Capabilities
5
6
### Image Generation
7
8
Generate images from text descriptions using state-of-the-art diffusion models.
9
10
```python { .api }
11
def generate(
12
prompt: str,
13
model: str,
14
n: int = 1,
15
steps: Optional[int] = None,
16
seed: Optional[int] = None,
17
height: Optional[int] = None,
18
width: Optional[int] = None,
19
**kwargs
20
) -> ImageResponse:
21
"""
22
Generate images from text prompts.
23
24
Args:
25
prompt: Text description of desired image
26
model: Image generation model identifier
27
n: Number of images to generate (1-4)
28
steps: Number of inference steps
29
seed: Random seed for reproducible results
30
height: Image height in pixels
31
width: Image width in pixels
32
33
Returns:
34
ImageResponse with generated image data
35
"""
36
```
37
38
### Async Image Generation
39
40
Asynchronous image generation for concurrent processing.
41
42
```python { .api }
43
async def generate(
44
prompt: str,
45
model: str,
46
**kwargs
47
) -> ImageResponse:
48
"""
49
Asynchronously generate images from prompts.
50
51
Returns:
52
ImageResponse with generated image data
53
"""
54
```
55
56
## Usage Examples
57
58
### Basic Image Generation
59
60
```python
61
from together import Together
62
import base64
63
from PIL import Image
64
import io
65
66
client = Together()
67
68
response = client.images.generate(
69
prompt="A serene mountain landscape at sunset with reflection in a lake",
70
model="stabilityai/stable-diffusion-xl-base-1.0",
71
n=1,
72
steps=20,
73
width=1024,
74
height=768
75
)
76
77
# Get base64 encoded image data
78
image_data = response.data[0].b64_json
79
80
# Decode and save image
81
image_bytes = base64.b64decode(image_data)
82
image = Image.open(io.BytesIO(image_bytes))
83
image.save("generated_landscape.png")
84
print("Image saved successfully!")
85
```
86
87
### Multiple Image Variations
88
89
```python
90
response = client.images.generate(
91
prompt="Abstract geometric patterns in vibrant colors",
92
model="stabilityai/stable-diffusion-xl-base-1.0",
93
n=4, # Generate 4 variations
94
steps=25,
95
width=512,
96
height=512,
97
seed=42 # For reproducible results
98
)
99
100
# Save all generated images
101
for i, image_data in enumerate(response.data):
102
image_bytes = base64.b64decode(image_data.b64_json)
103
image = Image.open(io.BytesIO(image_bytes))
104
image.save(f"abstract_art_{i+1}.png")
105
106
print(f"Generated and saved {len(response.data)} images")
107
```
108
109
### High-Quality Portrait Generation
110
111
```python
112
response = client.images.generate(
113
prompt="Professional headshot of a business executive, studio lighting, high resolution, photorealistic",
114
model="stabilityai/stable-diffusion-xl-base-1.0",
115
n=1,
116
steps=30,
117
width=768,
118
height=1024,
119
seed=123
120
)
121
122
image_data = response.data[0].b64_json
123
image_bytes = base64.b64decode(image_data)
124
image = Image.open(io.BytesIO(image_bytes))
125
image.save("professional_portrait.jpg", "JPEG", quality=95)
126
```
127
128
### Artistic Style Generation
129
130
```python
131
artistic_prompts = [
132
"Van Gogh style painting of a starry night over a cityscape",
133
"Minimalist line drawing of a cat, black ink on white paper",
134
"Watercolor painting of cherry blossoms in spring",
135
"Digital art cyberpunk city with neon lights"
136
]
137
138
for i, prompt in enumerate(artistic_prompts):
139
response = client.images.generate(
140
prompt=prompt,
141
model="stabilityai/stable-diffusion-xl-base-1.0",
142
n=1,
143
steps=25,
144
width=768,
145
height=768
146
)
147
148
image_bytes = base64.b64decode(response.data[0].b64_json)
149
image = Image.open(io.BytesIO(image_bytes))
150
image.save(f"artistic_style_{i+1}.png")
151
print(f"Generated: {prompt[:50]}...")
152
```
153
154
### Async Batch Image Generation
155
156
```python
157
import asyncio
158
from together import AsyncTogether
159
160
async def generate_images_async():
161
client = AsyncTogether()
162
163
prompts = [
164
"Futuristic robot in a workshop",
165
"Peaceful garden with butterflies",
166
"Abstract space scene with nebula",
167
"Vintage car on a country road"
168
]
169
170
tasks = [
171
client.images.generate(
172
prompt=prompt,
173
model="stabilityai/stable-diffusion-xl-base-1.0",
174
n=1,
175
steps=20,
176
width=512,
177
height=512
178
)
179
for prompt in prompts
180
]
181
182
responses = await asyncio.gather(*tasks)
183
184
for i, response in enumerate(responses):
185
image_bytes = base64.b64decode(response.data[0].b64_json)
186
image = Image.open(io.BytesIO(image_bytes))
187
image.save(f"async_generated_{i+1}.png")
188
189
print(f"Generated {len(responses)} images asynchronously")
190
191
asyncio.run(generate_images_async())
192
```
193
194
### Image Processing Utility Functions
195
196
```python
197
def save_generated_images(response: ImageResponse, base_filename: str = "generated"):
198
"""Save all images from an ImageResponse."""
199
saved_files = []
200
201
for i, image_data in enumerate(response.data):
202
filename = f"{base_filename}_{i+1}.png" if len(response.data) > 1 else f"{base_filename}.png"
203
204
image_bytes = base64.b64decode(image_data.b64_json)
205
image = Image.open(io.BytesIO(image_bytes))
206
image.save(filename)
207
saved_files.append(filename)
208
209
return saved_files
210
211
def display_image_info(response: ImageResponse):
212
"""Display information about generated images."""
213
print(f"Generated {len(response.data)} image(s)")
214
215
for i, image_data in enumerate(response.data):
216
image_bytes = base64.b64decode(image_data.b64_json)
217
image = Image.open(io.BytesIO(image_bytes))
218
print(f"Image {i+1}: {image.size[0]}x{image.size[1]} pixels, {image.mode} mode")
219
220
# Example usage
221
response = client.images.generate(
222
prompt="Majestic eagle soaring over mountains",
223
model="stabilityai/stable-diffusion-xl-base-1.0",
224
n=2,
225
steps=25
226
)
227
228
saved_files = save_generated_images(response, "eagle_mountain")
229
display_image_info(response)
230
print(f"Saved files: {saved_files}")
231
```
232
233
## Types
234
235
### Request Types
236
237
```python { .api }
238
class ImageRequest:
239
prompt: str
240
model: str
241
n: int = 1
242
steps: Optional[int] = None
243
seed: Optional[int] = None
244
height: Optional[int] = None
245
width: Optional[int] = None
246
```
247
248
### Response Types
249
250
```python { .api }
251
class ImageResponse:
252
created: int
253
data: List[ImageData]
254
255
class ImageData:
256
b64_json: str
257
revised_prompt: Optional[str] = None
258
```
259
260
## Supported Models
261
262
Popular image generation models available:
263
264
- `stabilityai/stable-diffusion-xl-base-1.0` - High-quality general purpose
265
- `stabilityai/stable-diffusion-2-1` - Versatile diffusion model
266
- `prompthero/openjourney` - Artistic and creative styles
267
- `wavymulder/Analog-Diffusion` - Analog photography aesthetic
268
- `22h/vintedois-diffusion-v0-1` - Vintage and retro styles