本指南演示如何通过 DeerAPI 使用 Gemini 图像模型进行图像生成与编辑。
快速开始
替换配置
将 Base URL 设置为 https://api.deerapi.com,使用你的 DeerAPI Key 作为认证凭证。
发起请求
通过 SDK 或 HTTP 请求调用 generateContent 接口,设置 responseModalities 包含 "IMAGE"。
一、文生图
通过文本提示生成图片。将描述文字放入 contents.parts[].text 中。
from google import genai
from google.genai import types
client = genai.Client(
api_key = "<DEERAPI_KEY>" ,
http_options = { "api_version" : "v1beta" , "base_url" : "https://api.deerapi.com" }
)
response = client.models.generate_content(
model = "gemini-3.1-flash-image-preview" ,
contents = [ "一位女性倚靠在传统中式建筑的木栏杆上,身穿蓝色旗袍,头戴花饰" ],
config = types.GenerateContentConfig(
response_modalities = [ "TEXT" , "IMAGE" ]
)
)
for part in response.parts:
if part.text is not None :
print (part.text)
elif part.inline_data is not None :
image = part.as_image()
image.save( "output.png" )
print ( "图片已保存至 output.png" )
import { GoogleGenAI } from "@google/genai" ;
import * as fs from "fs" ;
const ai = new GoogleGenAI ({
apiKey: "<DEERAPI_KEY>" ,
httpOptions: { apiVersion: "v1beta" , baseUrl: "https://api.deerapi.com" }
});
const response = await ai . models . generateContent ({
model: "gemini-3.1-flash-image-preview" ,
contents: [{ parts: [{ text: "一位女性倚靠在传统中式建筑的木栏杆上" }] }],
config: { responseModalities: [ "TEXT" , "IMAGE" ] }
});
for ( const part of response . candidates [ 0 ]. content . parts ) {
if ( part . text ) {
console . log ( part . text );
} else if ( part . inlineData ) {
fs . writeFileSync ( "output.png" , Buffer . from ( part . inlineData . data , "base64" ));
console . log ( "图片已保存至 output.png" );
}
}
curl -s -X POST \
"https://api.deerapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "Authorization: Bearer $DEERAPI_KEY " \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "A cute baby sea otter floating in the ocean"}]}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}' | jq -r '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' \
| head -1 | base64 --decode > output.png
响应格式
响应在 candidates[0].content.parts 中返回,包含:
文字描述 :{"text": "..."}
图像数据 :{"inlineData": {"mimeType": "image/png", "data": "<base64>"}}
二、单图生图
上传一张图片(Base64 编码),结合文本提示生成新图。
from google import genai
from google.genai import types
from PIL import Image
client = genai.Client(
api_key = "<DEERAPI_KEY>" ,
http_options = { "api_version" : "v1beta" , "base_url" : "https://api.deerapi.com" }
)
input_image = Image.open( "input.png" )
response = client.models.generate_content(
model = "gemini-3.1-flash-image-preview" ,
contents = [ "将背景变为蓝色" , input_image],
config = types.GenerateContentConfig(
response_modalities = [ "TEXT" , "IMAGE" ]
)
)
for part in response.parts:
if part.text is not None :
print (part.text)
elif part.inline_data is not None :
image = part.as_image()
image.save( "edited.png" )
print ( "图片已保存至 edited.png" )
# 先将图片转为 Base64
BASE64_IMAGE = $( base64 < input.png )
curl -s -X POST \
"https://api.deerapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "Authorization: Bearer $DEERAPI_KEY " \
-H "Content-Type: application/json" \
-d "{
\" contents \" : [{
\" role \" : \" user \" ,
\" parts \" : [
{ \" text \" : \" 将背景变为蓝色 \" },
{ \" inline_data \" : { \" mime_type \" : \" image/png \" , \" data \" : \" $BASE64_IMAGE \" }}
]
}],
\" generationConfig \" : {
\" responseModalities \" : [ \" TEXT \" , \" IMAGE \" ]
}
}" | jq -r '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' \
| head -1 | base64 --decode > edited.png
inline_data.data 中只放纯 Base64 数据,不要包含 data:image/png;base64, 前缀。
三、多图生图
方式一:拼图输入
将多张图片拼接为一张,作为单图输入。
输出效果:
方式二:多图 Base64 传参
在 parts 数组中传入多个 inline_data 对象。Gemini 3 系列支持最多 14 张参考图。
from google import genai
from google.genai import types
from PIL import Image
client = genai.Client(
api_key = "<DEERAPI_KEY>" ,
http_options = { "api_version" : "v1beta" , "base_url" : "https://api.deerapi.com" }
)
response = client.models.generate_content(
model = "gemini-3.1-flash-image-preview" ,
contents = [
"融合三张图片,输出高清图片" ,
Image.open( "image1.png" ),
Image.open( "image2.png" ),
Image.open( "image3.png" ),
],
config = types.GenerateContentConfig(
response_modalities = [ "TEXT" , "IMAGE" ],
image_config = types.ImageConfig(
aspect_ratio = "1:1" ,
image_size = "2K"
),
)
)
for part in response.parts:
if part.text is not None :
print (part.text)
elif part.inline_data is not None :
image = part.as_image()
image.save( "merged.png" )
print ( "图片已保存至 merged.png" )
curl -s -X POST \
"https://api.deerapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "Authorization: Bearer $DEERAPI_KEY " \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"role": "user",
"parts": [
{"text": "融合三张图片,输出高清图片"},
{"inline_data": {"mime_type": "image/jpeg", "data": "<图片1的Base64>"}},
{"inline_data": {"mime_type": "image/jpeg", "data": "<图片2的Base64>"}},
{"inline_data": {"mime_type": "image/jpeg", "data": "<图片3的Base64>"}}
]
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}' | jq -r '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' \
| head -1 | base64 --decode > merged.png
多图融合效果参考:
从响应中提取图片
# 使用 google-genai SDK(推荐)
for part in response.parts:
if part.inline_data is not None :
image = part.as_image()
image.save( "output.png" )
# 或手动解码 Base64
import base64
b64_data = "<base64-string>"
with open ( "output.png" , "wb" ) as f:
f.write(base64.b64decode(b64_data))
const fs = require ( "fs" );
const b64Data = "<base64-string>" ;
fs . writeFileSync ( "output.png" , Buffer . from ( b64Data , "base64" ));
宽高比与分辨率控制
通过 imageConfig 参数控制输出图片的尺寸:
config = types.GenerateContentConfig(
response_modalities = [ "TEXT" , "IMAGE" ],
image_config = types.ImageConfig(
aspect_ratio = "16:9" , # 支持:1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9 等
image_size = "2K" # 支持:512px, 1K, 2K, 4K(必须大写 K)
),
)
gemini-2.5-flash-image 固定 1024px 分辨率,不支持 image_size 参数。
常见问题
DeerAPI 使用 Bearer Token 认证。curl 请求使用 Authorization: Bearer $DEERAPI_KEY,SDK 中直接传入 api_key。
指定风格关键词(如”赛博朋克、胶片质感、油画风格”)、画幅、主体、背景、光线、细节程度等,有助于提升生成效果。使用英文提示通常效果更好。
inline_data.data 中只放纯 Base64 数据,不要包含 data:image/png;base64, 前缀。
将 responseModalities 设置为 ["IMAGE"](不包含 "TEXT"),可以避免模型只输出文本不生成图片的情况。
4xx 错误多为请求参数或认证问题(检查 Key、模型名、JSON 格式);5xx 为服务端问题(可稍后重试)。
参考文档