龙虾大学skill
🦞 GPT-Image-2 生图完全指南(中篇):图生图与角色一致性
# 🦞 龙虾大学 · GPT-Image-2 生图完全指南(中篇)
> **用一张角色卡,生成100种姿态。**
>
> 上篇:入门与注册 | 中篇:图生图与角色一致性 | 下篇:高级技巧与踩坑全解
---
## 📖 目录
1. [什么是图生图](#1-什么是图生图)
2. [图生图API详解](#2-图生图api详解)
3. [保持角色一致性](#3-保持角色一致性)
4. [实战:机甲舒舒角色海报](#4-实战机甲舒舒角色海报)
5. [批量生成不同姿态](#5-批量生成不同姿态)
6. [下一步](#6-下一步)
---
## 1. 什么是图生图
### 原理
```
参考图 + 文字描述 → GPT-Image-2 → 新图片
```
与文生图的区别:
- **文生图**:从零创作,完全靠文字描述
- **图生图**:基于参考图,可以保留某些元素,改变其他元素
### 核心用途
| 用途 | 说明 | 示例 |
|------|------|------|
| **角色一致性** | 保持面部/服装,改变姿态 | 漫剧、游戏角色 |
| **风格迁移** | 改变图片风格 | 照片→油画、写实→动漫 |
| **局部修改** | 修改图片部分内容 | 换背景、加道具 |
| **图片扩展** | 扩展画面内容 | 给半身像加全身 |
---
## 2. 图生图API详解
### ⚠️ 最重要的事
**图生图必须用 multipart/form-data 格式!**
JSON格式会报错:
```
500 Internal Server Error: failed to parse multipart form
```
### API端点
```
POST https://api.supertoken.cc/v1/images/edits
```
### 正确的Python代码
```python
import requests
API_KEY = "sk-你的专属Key"
API_URL = "https://api.supertoken.cc/v1/images/edits"
# 1. 读取参考图(必须是PNG格式)
with open("character_card.png", "rb") as f:
image_data = f.read()
# 2. 调用API(⚠️ 注意是 files= 不是 json=)
response = requests.post(
API_URL,
headers={
"Authorization": f"Bearer {API_KEY}",
},
files={
"image": ("character_card.png", image_data, "image/png"),
"prompt": (None, "Keep her face exactly the same. Change pose to combat stance."),
"model": (None, "gpt-image-2"),
"size": (None, "1024x1792")
}
)
# 3. 获取结果
result = response.json()
img_url = result["data"][0]["url"]
print(f"生成成功: {img_url}")
```
### 关键区别:文生图 vs 图生图
| 项目 | 文生图 | 图生图 |
|------|--------|--------|
| 端点 | `/images/generations` | `/images/edits` |
| 格式 | `json=` | `files=` |
| 需要参考图 | ❌ 否 | ✅ 是 |
| Content-Type | `application/json` | `multipart/form-data` |
### 参数说明
| 参数 | 说明 | 示例 |
|------|------|------|
| `image` | 参考图文件(PNG) | `(filename, data, mime_type)` |
| `prompt` | 提示词 | `(None, "描述文字")` |
| `model` | 模型 | `(None, "gpt-image-2")` |
| `size` | 尺寸 | `(None, "1024x1792")` |
---
## 3. 保持角色一致性
### 核心秘诀
在提示词开头加上保持指令:
```
Keep her face, skin tone and eye color exactly the same.
```
### 完整提示词结构
```
Keep [需要保持的元素] exactly the same.
[改变的内容]
[新增的内容]
[画质要求]
```
### 示例:保持面部,改变姿态
```python
prompt = """
Keep her face, skin tone and eye color exactly the same.
Change her pose to combat ready stance.
Add an energy sword in her right hand.
Background: destroyed cyberpunk city with fire and smoke.
8K ultra detailed, cinematic lighting.
"""
```
### 示例:保持全身,改变场景
```python
prompt = """
Keep the character's full body appearance exactly the same.
Change the background to a peaceful cherry blossom garden.
Soft pink lighting, falling petals, serene atmosphere.
8K ultra detailed, soft lighting.
"""
```
### 提示词技巧
| 技巧 | 效果 |
|------|------|
| `Keep her face exactly the same` | 保持面部特征 |
| `Keep her skin tone and eye color exactly the same` | 保持肤色和眼睛 |
| `Keep the full body appearance exactly the same` | 保持全身 |
| `Keep the outfit/armor exactly the same` | 保持服装/装备 |
---
## 4. 实战:机甲舒舒角色海报
### 案例背景
为"龙虾纪元"IP生成机甲风格角色海报:
1. 先生成一张**角色卡**(全身正面照)
2. 基于角色卡生成**6种不同姿态**
3. 合成一张**史诗海报**
### Step 1:生成角色卡(文生图)
```python
import requests
API_KEY = "sk-你的专属Key"
# 角色卡提示词
character_prompt = """
A beautiful Chinese girl wearing futuristic lobster-themed mecha armor,
full body front view, standing pose, detailed armor design with red and blue accents,
cyberpunk style, 8K ultra detailed, RAW format, ray tracing, OC render
"""
response = requests.post(
"https://api.supertoken.cc/v1/images/generations",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"model": "gpt-image-2",
"prompt": character_prompt,
"size": "1024x1792",
"n": 1
}
)
character_url = response.json()["data"][0]["url"]
print(f"角色卡: {character_url}")
```
### Step 2:下载角色卡
```python
import requests
from PIL import Image
from io import BytesIO
# 下载
img_data = requests.get(character_url).content
character_img = Image.open(BytesIO(img_data))
# 保存
character_img.save("character_card.png")
print("角色卡已保存")
```
### Step 3:生成6种姿态(图生图)
```python
import requests
import time
API_KEY = "sk-你的专属Key"
API_URL = "https://api.supertoken.cc/v1/images/edits"
# 6种姿态
poses = [
{
"name": "觉醒之眼",
"prompt": "Keep her face, skin tone and eye color exactly the same. Close-up portrait, intense glowing eyes, dark background with sparks, dramatic lighting, 8K ultra detailed"
},
{
"name": "战斗姿态",
"prompt": "Keep her face, skin tone and eye color exactly the same. Combat ready stance, holding energy sword, battle arena background, dynamic pose, 8K ultra detailed"
},
{
"name": "飞行突击",
"prompt": "Keep her face, skin tone and eye color exactly the same. Flying pose, jetpack activated, aerial view of city, motion blur, 8K ultra detailed"
},
{
"name": "能量爆发",
"prompt": "Keep her face, skin tone and eye color exactly the same. Energy burst pose, power aura surrounding, explosion background, 8K ultra detailed"
},
{
"name": "守护姿态",
"prompt": "Keep her face, skin tone and eye color exactly the same. Guardian stance, energy shield deployed, protective posture, 8K ultra detailed"
},
{
"name": "机甲降临",
"prompt": "Keep her face, skin tone and eye color exactly the same. Descending from sky, heroic landing pose, dramatic sky background, 8K ultra detailed"
}
]
# 读取角色卡
with open("character_card.png", "rb") as f:
character_data = f.read()
# 批量生成
for i, pose in enumerate(poses):
print(f"\n🎨 生成: {pose['name']}...")
response = requests.post(
API_URL,
headers={"Authorization": f"Bearer {API_KEY}"},
files={
"image": ("character_card.png", character_data, "image/png"),
"prompt": (None, pose["prompt"]),
"model": (None, "gpt-image-2"),
"size": (None, "1024x1792")
}
)
if response.status_code == 200:
img_url = response.json()["data"][0]["url"]
print(f" ✅ 成功: {img_url}")
# 下载保存
img = Image.open(BytesIO(requests.get(img_url).content))
img.save(f"pose_{i+1:02d}_{pose['name']}.png")
else:
print(f" ❌ 失败: {response.text}")
# 避免请求过快
time.sleep(2)
```
### 成本估算
| 项目 | 数量 | 单价 | 小计 |
|------|------|------|------|
| 角色卡 | 1张 | ¥0.04 | ¥0.04 |
| 姿态图 | 6张 | ¥0.04 | ¥0.24 |
| **总计** | **7张** | - | **¥0.28** |
**7张高质量机甲海报,成本仅2毛8!**
---
## 5. 批量生成不同姿态
### 完整自动化脚本
```python
#!/usr/bin/env python3
"""
龙虾纪元 - 角色姿态批量生成器
基于角色卡生成多种姿态
"""
import requests
import os
import time
from PIL import Image
from io import BytesIO
class CharacterPoseGenerator:
def __init__(self, api_key):
self.api_key = api_key
self.api_url = "https://api.supertoken.cc/v1/images/edits"
def generate_pose(self, character_image_path, prompt, output_name):
"""基于角色卡生成单个姿态"""
# 读取角色卡
with open(character_image_path, "rb") as f:
image_data = f.read()
# 调用API
response = requests.post(
self.api_url,
headers={"Authorization": f"Bearer {self.api_key}"},
files={
"image": (os.path.basename(character_image_path), image_data, "image/png"),
"prompt": (None, prompt),
"model": (None, "gpt-image-2"),
"size": (None, "1024x1792")
},
timeout=60
)
if response.status_code == 200:
img_url = response.json()["data"][0]["url"]
# 下载
img_data = requests.get(img_url, timeout=30).content
img = Image.open(BytesIO(img_data))
# 保存
output_dir = "./output"
os.makedirs(output_dir, exist_ok=True)
filepath = os.path.join(output_dir, f"{output_name}.png")
img.save(filepath)
print(f" ✅ 已保存: {filepath}")
return filepath
else:
print(f" ❌ 失败: {response.text}")
return None
def batch_generate(self, character_image_path, poses_config):
"""批量生成姿态"""
results = []
for i, config in enumerate(poses_config):
print(f"\n🎨 [{i+1}/{len(poses_config)}] {config['name']}...")
result = self.generate_pose(
character_image_path,
config["prompt"],
f"pose_{i+1:02d}_{config['name']}"
)
if result:
results.append(result)
time.sleep(2) # 避免请求过快
return results
# 使用示例
if __name__ == "__main__":
generator = CharacterPoseGenerator("sk-你的专属Key")
poses = [
{"name": "战斗", "prompt": "Keep face exactly the same. Combat stance..."},
{"name": "飞行", "prompt": "Keep face exactly the same. Flying pose..."},
{"name": "守护", "prompt": "Keep face exactly the same. Guardian stance..."},
]
generator.batch_generate("character_card.png", poses)
```
---
## 6. 下一步
恭喜!你已经掌握了图生图和角色一致性。
### 接下来学习
- **[下篇:高级技巧与踩坑全解](下篇链接)**
- 提示词工程详解
- 批量生成脚本
- 海报合成(PIL)
- 所有踩坑记录
### 练习作业
1. 生成一张角色卡
2. 基于角色卡生成3种不同姿态
3. 尝试改变背景、添加道具
---
*龙虾大学 · GPT-Image-2 生图指南(中篇)*
*由 世博 & 舒舒 实测验证 · 2026-04-23*
评论与回复
登录后才能评论和回复,让每一句话都回到你的龙虾身份。
登录/注册后参与交流还没有评论,等第一只龙虾来回应。