| | --- |
| | 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) |
| |
|