File size: 7,290 Bytes
0de5f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ab02f1
0de5f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ab02f1
0de5f43
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
---
license: mit
tags:
  - tutorial
  - crazyrouter
  - dall-e
  - flux
  - image-generation
  - ai-art
  - stable-diffusion
  - midjourney
  - llm
language:
  - en
  - zh
---

# 🎨 Crazyrouter AI 图像生成指南

> 通过 Crazyrouter API 调用 DALL-E 3、Flux 等图像生成模型

[Crazyrouter](https://crazyrouter.com/?utm_source=huggingface&utm_medium=tutorial&utm_campaign=dev_community) 不仅支持文本模型,还支持图像生成模型。一个 API Key 即可调用多种 AI 绘画模型。

---

## 支持的图像模型

| 模型 | 提供商 | 特点 |
|------|--------|------|
| `dall-e-3` | OpenAI | 高质量、理解复杂提示词 |
| `dall-e-2` | OpenAI | 快速、便宜 |
| `flux-schnell` | Black Forest Labs | 开源、速度快 |
| `flux-dev` | Black Forest Labs | 高质量开源模型 |

---

## 快速开始

### Python

```python
from openai import OpenAI

client = OpenAI(
    base_url="https://crazyrouter.com/v1",
    api_key="sk-your-crazyrouter-key"
)

response = client.images.generate(
    model="dall-e-3",
    prompt="A cute robot serving coffee in a futuristic cafe, digital art style",
    size="1024x1024",
    quality="standard",
    n=1,
)

image_url = response.data[0].url
print(f"Image URL: {image_url}")
```

### cURL

```bash
curl https://crazyrouter.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-crazyrouter-key" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A serene Japanese garden with cherry blossoms, watercolor painting",
    "size": "1024x1024",
    "n": 1
  }'
```

### Node.js

```javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://crazyrouter.com/v1",
  apiKey: "sk-your-crazyrouter-key",
});

const response = await client.images.generate({
  model: "dall-e-3",
  prompt: "A cyberpunk cityscape at night with neon lights, 4K detailed",
  size: "1024x1024",
  n: 1,
});

console.log(response.data[0].url);
```

---

## DALL-E 3 详细用法

### 尺寸选项

| 尺寸 | 用途 |
|------|------|
| `1024x1024` | 正方形(默认) |
| `1024x1792` | 竖版(手机壁纸、海报) |
| `1792x1024` | 横版(桌面壁纸、Banner) |

### 质量选项

| 质量 | 说明 |
|------|------|
| `standard` | 标准质量(默认,更快) |
| `hd` | 高清质量(更细腻,更慢) |

### 风格选项

| 风格 | 说明 |
|------|------|
| `vivid` | 鲜艳风格(默认) |
| `natural` | 自然风格 |

### 完整示例

```python
response = client.images.generate(
    model="dall-e-3",
    prompt="A majestic dragon flying over a medieval castle at sunset",
    size="1792x1024",    # 横版
    quality="hd",         # 高清
    style="vivid",        # 鲜艳
    n=1,
)
```

---

## 批量生成

```python
prompts = [
    "A minimalist logo for a tech startup, blue and white",
    "A cozy reading nook with warm lighting, illustration style",
    "An astronaut playing guitar on the moon, digital art",
    "A steampunk mechanical butterfly, detailed 3D render",
]

for i, prompt in enumerate(prompts):
    response = client.images.generate(
        model="dall-e-3",
        prompt=prompt,
        size="1024x1024",
        n=1,
    )
    print(f"Image {i+1}: {response.data[0].url}")
```

---

## 下载图片

```python
import requests

response = client.images.generate(
    model="dall-e-3",
    prompt="A beautiful sunset over the ocean",
    size="1024x1024",
    n=1,
)

image_url = response.data[0].url

# 下载保存
img_data = requests.get(image_url).content
with open("sunset.png", "wb") as f:
    f.write(img_data)
print("Image saved as sunset.png")
```

---

## 在客户端中使用

### Cherry Studio

Cherry Studio 内置 AI 绘画功能,配置 Crazyrouter 后可以直接在客户端中生成图片。

### ChatBox

ChatBox 支持图片生成,在对话中输入绘画提示词即可。

### NextChat / LobeChat

这些客户端支持 DALL-E 插件,配置 Crazyrouter API 后可以在对话中生成图片。

---

## 提示词技巧

### 基础结构

```
[主体] + [风格] + [细节] + [氛围]
```

### 示例

| 提示词 | 效果 |
|--------|------|
| `A cat wearing a space suit, digital art, highly detailed` | 太空猫,数字艺术风格 |
| `Minimalist mountain landscape, Japanese ink wash painting` | 极简山水,水墨画风格 |
| `Product photo of a sleek smartphone, studio lighting, white background` | 产品摄影风格 |
| `Isometric view of a tiny coffee shop, pixel art, warm colors` | 等距像素风咖啡店 |

### 风格关键词

- **写实**: `photorealistic, 8K, detailed, professional photography`
- **插画**: `illustration, digital art, concept art, artstation`
- **水彩**: `watercolor painting, soft colors, artistic`
- **像素**: `pixel art, retro, 16-bit, game style`
- **3D**: `3D render, octane render, blender, cinema 4D`
- **动漫**: `anime style, manga, studio ghibli`
- **极简**: `minimalist, clean, simple, flat design`

---

## 与文本模型结合

让 AI 先生成提示词,再用来绘画:

```python
# Step 1: 用文本模型生成优化的提示词
chat_response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": "Generate a detailed DALL-E prompt for: a cozy winter cabin. Include style, lighting, and mood details. Output only the prompt."
    }]
)
optimized_prompt = chat_response.choices[0].message.content

# Step 2: 用优化后的提示词生成图片
image_response = client.images.generate(
    model="dall-e-3",
    prompt=optimized_prompt,
    size="1792x1024",
    quality="hd",
    n=1,
)
print(f"Prompt: {optimized_prompt}")
print(f"Image: {image_response.data[0].url}")
```

---

## ComfyUI 集成

如果你使用 ComfyUI 进行更高级的图像生成工作流,可以通过 Crazyrouter API 获取文本提示词优化:

```python
# 用 Crazyrouter 的文本模型优化 ComfyUI 的提示词
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "system",
        "content": "You are a Stable Diffusion prompt expert. Convert user descriptions into optimized SD prompts with positive and negative prompts."
    }, {
        "role": "user",
        "content": "A beautiful girl in a flower garden"
    }]
)
print(response.choices[0].message.content)
# Output: positive prompt + negative prompt for ComfyUI/SD
```

---

## 价格参考

| 模型 | 尺寸 | 质量 | 价格/张 |
|------|------|------|---------|
| DALL-E 3 | 1024x1024 | standard | ~$0.04 |
| DALL-E 3 | 1024x1024 | hd | ~$0.08 |
| DALL-E 3 | 1792x1024 | standard | ~$0.08 |
| DALL-E 3 | 1792x1024 | hd | ~$0.12 |
| DALL-E 2 | 1024x1024 | - | ~$0.02 |

*通过 Crazyrouter 调用,价格可能更优惠。*

---

## 链接

- 🌐 [Crazyrouter](https://crazyrouter.com/?utm_source=huggingface&utm_medium=tutorial&utm_campaign=dev_community) — 获取 API Key
- 🤖 [在线 Demo](https://huggingface.co/spaces/xujfcn/Crazyrouter-Demo)
- 📖 [快速入门](https://huggingface.co/xujfcn/Crazyrouter-Getting-Started)
- 💻 [编程工具配置](https://huggingface.co/xujfcn/Crazyrouter-AI-Coding-Tools)
- 💬 [Telegram 社区](https://t.me/crazyrouter)
- 🐦 [Twitter @metaviiii](https://twitter.com/metaviiii)