跳至主要内容

Seedream 4.5:Python 全面指南

面向实践的 Python 指南,带您使用字节跳动的 Seedream 4.5 图像模型,涵盖批量生成、文本渲染、多图编辑与提示词最佳实践。
· 10分钟 阅读

使用 AI 探索

BytePlus 刚刚发布了其最新图像生成模型 Seedream 4.5。作为字节跳动的企业技术部门,该公司将先进的 AI 能力带给全球企业,延续了如 TikTok 等平台背后的创新。

本文将通过 API 集成,手把手教您如何使用 Python 调用 Seedream 4.5,进行程序化图像生成。

我建议先学习 Generative AI Concepts 课程,作为理解 Seedream 等图像生成模型背后技术的良好入门。如果您也对其他图像生成模型感兴趣,不妨阅读我们关于谷歌 Nano Banana 2 的指南。

什么是 Seedream 4.5

Seedream 4.5 是 BytePlus AI 图像生成模型的最新迭代。

最新版本增强了主体一致性、细小文字细节清晰度,并改进了多图混合。同时具备更强的指令理解、更好的空间推理,以及更先进的逻辑推断能力。

该模型还可以生成高质量的 4K 图像,这是令人欢迎的提升,因为竞品模型往往难以生成高分辨率图像。在成本方面也具备竞争力,每张图仅需 4 美分即可生成。

别忘了查看我们关于字节跳动新视频生成模型 Seedance 2.0 的文章。

Seedream 4.5 API 设置

闲话少说,让我们开始使用 Seedream 4.5 的 API。

创建 API 密钥

要通过其 API 在 Python 中使用 Seedream 4.5,我们首先需要创建一个 API 密钥。

  1. 前往 其官方网站,点击右下角的 Get API 按钮并登录。

  2. 登录后,右上角有一个 Access API 按钮。点击并创建 API 密钥。创建 API 密钥需要验证手机号并提供账单信息。但这并非订阅,因此不会按月收费,只对我们的 API 请求计费。

  3. 在与我们编写 Python 代码相同的文件夹中创建名为 .env 的文件,并按以下格式将密钥复制进去:

ARK_API_KEY=<paste_key_here>

在使用模型之前,请务必在 BytePlus ModelArk 控制台中激活该模型。这一步与创建 API 密钥相互独立。操作步骤如下:

  1. 在左侧边栏进入 Model activation。

  2. 在中间选择 Media 作为正确的模型分类。

  3. 在模型 ByteDance-Seedream-4.5 所在行右侧点击 Activate。

  4. 在弹出的窗口中确认。

Python 依赖安装

要与 Seedream 4.5 API 交互,我们需要安装一些 Python 包:

  • byteplussdkarkruntime:官方 Python 包,使我们可以向 Seedream 4.5 发起 API 请求,属于 BytePlus Python SDK 的一部分。

  • 若干辅助包,用于从 .env 文件加载 API 密钥、进行异步 HTTP 请求和配置校验。

使用以下命令安装这些包:

pip install byteplus-python-sdk-v2 python-dotenv httpx requests typing_extensions pydantic

使用 Seedream 4.5 生成图像

先从文本提示词生成图像学起。如果您看过我们其他关于 Python AI API 的教程,比如 GPT-Image-1 或 Grok Imagine,您可能已经熟悉典型的工作流:

  1. 导入依赖包。

  2. 从 .env 文件加载 API 密钥。

  3. 使用官方 API 包创建客户端。

  4. 发送 API 请求。

Seedream 4.5 的用法完全相同。以下是一个生成图像的示例脚本。

# 1. Import the necessary packages
from dotenv import load_dotenv
from byteplussdkarkruntime import Ark 
import os
# 2. Load the API key
load_dotenv()
# 3. Create the client
client = Ark(
    base_url="https://ark.ap-southeast.bytepluses.com/api/v3", 
    api_key=os.getenv("ARK_API_KEY"), 
)
# 4. Send the API request
prompt = """
Photorealistic anthropomorphic animals hanging out in a coffee shop with no humans.
"""
 
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128",
    prompt=prompt,
    watermark=False,
)
 
image_url = imagesResponse.data[0].url
print(image_url)

在这个示例中,我们使用的提示词是:

Photorealistic anthropomorphic animals hanging out in a coffee shop with no humans.

模型生成的结果如下:

AI-generated Seedream 4.5 image: photorealistic anthropomorphic animals in a cozy café — fox sipping a latte with art, rabbit reading, and cat at the espresso bar in warm sunlight.

下载生成的图像

上述脚本会在终端输出生成图像的 URL。这并不是最方便的工作流。更好的做法是创建一个函数,将生成的图像下载到本地文件夹。

import requests
from urllib.parse import urlparse
import os
def download_image(url):
    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()
        parsed_url = urlparse(url)
        filename = os.path.basename(parsed_url.path)
        if not filename:
            filename = "downloaded_image"
        with open(filename, "wb") as f:
            for chunk in response.iter_content(1024):
                f.write(chunk)
        print(f"Image saved to: {filename}")
    except Exception as e:
        print(f"Error downloading image: {e}")

我建议新建一个名为 utils.py 的脚本,将该函数放入其中。然后在生成图像的脚本中,添加以下代码以下载图像:

import utils
utils.download_image(image_url)

指定图像尺寸

前面的示例除了将 watermark 设置为 False 隐藏水印,其他均使用了 Seedream 4.5 的默认参数。

Seedream 4.5 提供了两种方式通过 size 参数指定输出图像的尺寸。

第一种方式是指定生成图像的分辨率,将 size 设为 "2K" 或 "4K",并在提示词中用自然语言描述所需的纵横比、形状或用途,让模型自动确定宽高。默认值为 "2K"。

示例如下:

prompt = """
Portait Instagram image to be used to promote travel to Tokyo, Japan.
"""
 
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128",
    prompt=prompt,
    size="4K",
    watermark=False,
)

或者,我们也可以用类似 "3750x1250" 的字符串以像素指定图像尺寸,但需要遵循两条规则:

  1. 总像素(宽 × 高)需介于 360 万到 1670 万之间。
  2. 纵横比(宽 ÷ 高)需介于 1/16 到 16 之间。

若只满足其中一条规则,则尺寸无效。为避免手动计算,我建议要么使用上面的分辨率方法,要么选用下列任一有效尺寸:

Seedream 4.5 API image generation table showing recommended pixel sizes by aspect ratio: 1:1 2048x2048, 4:3 2304x1728, 3:4 1728x2304, 16:9 2560x1440, 9:16 1440x2560, 3:2 2496x1664, 2:3 1664x2496, 21:9 3024x1296.

批量生成

Seedream 4.5 包含批量生成模式,可用于生成一组主题相关的图像。例如,这对生成分镜或围绕同一主题的视觉素材很有用。

要生成一批图像,我们需要在 API 请求中提供 sequential_image_generation 和 sequential_image_generation_options 参数。以下是生成 4 张图像的示例:

from byteplussdkarkruntime.types.images.images import SequentialImageGenerationOptions
prompt = """
Generate a series of four visually coherent and stylistically consistent images illustrating these stages of a human life: Child, Adolescent, Adult, and Elderly. Make sure it's the same person in the four images.
"""
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    size="2K",
    sequential_image_generation="auto",
    sequential_image_generation_options=SequentialImageGenerationOptions(max_images=4),
    response_format="url",
    watermark=False
)

使用批量生成功能时,返回的响应格式会不同,包含所有图像。我们可以如下遍历并下载它们:

# Iterate through all image data
for image in imagesResponse.data:
    utils.download_image(image.url)

我使用了以下提示词进行尝试:

Generate a series of four visually coherent and stylistically consistent images illustrating these stages of a human life: Child, Adolescent, Adult, and Elderly. Make sure it's the same person in the four images.

不过不知为何,第四张图总是打破一致性,生成了一位女性(我试了多次)。

Seedream 4.5 AI sequential image generation example: four-panel collage of the same person across life stages—child flying a kite, teen skater by graffiti, adult man working at a laptop, elderly woman sipping tea on a porch.

如需生成不同数量的图像,只需在 API 请求中修改 max_images 的数值。该数值可为 1 到 15(含)之间的任意整数。例如,生成 6 张图像:

imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    size="2K",
    sequential_image_generation="auto",
    sequential_image_generation_options=SequentialImageGenerationOptions(max_images=6),
    response_format="url",
    watermark=False
)

文本生成

我们来测试一下 Seedream 4.5 的文本生成能力。测试中,我尝试在未明确指定要渲染的文字内容的情况下,看看它能否生成包含数学证明的图像。这也同时考察模型的上下文理解能力。

我使用的提示词是:

Generate an image of a math professor standing in front of a blackboard. On the blackboard, there is a written proof that the square root of 2 is irrational.

结果如下:

BytePlus Seedream 4.5 AI-generated illustration of a math professor pointing at a chalkboard with a clear, step-by-step proof that the square root of 2 is irrational, showcasing sharp text rendering.

我对这个结果相当满意。证明不仅正确,模型还能很好地处理大量文本。

使用 Seedream 4.5 编辑图像

本节我们学习如何编辑图像。使用 Seedream 4.5 编辑图像的简单方式是通过 image 参数提供图像 URL,并在提示词中描述要进行的修改。

以下是完整脚本示例:

from dotenv import load_dotenv
from byteplussdkarkruntime import Ark 
import os
import utils
load_dotenv()
client = Ark(
    base_url="https://ark.ap-southeast.bytepluses.com/api/v3", 
    api_key=os.getenv('ARK_API_KEY'), 
)
image = "https://images.pexels.com/photos/34408249/pexels-photo-34408249.jpeg"
prompt = """
Make in snowy.
"""
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    image=image,
    response_format="url",
    watermark=False,
    size="1664x2496"
) 
 
image_url = imagesResponse.data[0].url
utils.download_image(image_url)

在这个例子中,我使用了Michael Fischer 在 Pexels 上公开可用的照片,并用 Seedream 4.5 让它变成了雪景。

我得到的结果如下:

Seedream 4.5 API image editing before-and-after: rustic alpine cabin by a mountain lake, wooden fence and ground transformed from autumn greens to snowy winter using the prompt “Make it snowy.”

请注意,在上述示例中,我们显式指定了输出尺寸 "1664x2496",以匹配输入图像的纵横比。与大多数 AI 图像模型不同,Seedream 4.5 不会自动按输入的相同尺寸生成图像。当我省略 size 参数时,输出是一张方图。

编辑本地图像

使用图像 URL 是向 Seedream 4.5 提供图像的最简单方式。但如果图像尚未托管,这会比较麻烦。

Seedream API 也允许我们轻松使用本地图像。为此,需要将其加载为 base64 图像。下面是一个实现该功能的函数:

import mimetypes
import base64
def load_image_as_base64(file_path):
    mime_type, _ = mimetypes.guess_type(file_path)
if mime_type is None or not mime_type.startswith("image/"):
        raise ValueError("File is not a recognized image type")
with open(file_path, "rb") as f:
        encoded = base64.b64encode(f.read()).decode("utf-8")
return f"data:{mime_type};base64,{encoded}"

建议将它添加到包含下载图像函数的 utils.py 文件中,以便在主脚本中轻松使用。

假设我们在同一文件夹中有一张名为 motard.jpeg 的本地图像。下面展示如何加载并编辑该图像:

image = utils.load_image_as_base64("motard.jpeg")
prompt = """
Remove the sunglasses and add a helmet.
"""
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    image=image,
    response_format="url",
    watermark=False
) 

Seedream 4.5 image editing demo: side-by-side motorcyclist on a black bike, before with sunglasses and no helmet, after with helmet added, illustrating the prompt “Remove the sunglasses and add a helmet.”

多图输入编辑

Seedream 4.5 支持多张图像作为输入。可用来通过融合这些图像的风格和视觉元素来生成新图像。

在一次 API 请求中提供多张图像非常简单,只需以列表形式提交即可。以下示例将人物图与服装图组合,生成该人物穿着该套服装的图像:

image1 = utils.load_image_as_base64("woman.jpeg")
image2 = utils.load_image_as_base64("outfit.jpeg")
prompt = """
Make the woman from image 1 wear the outfit in image 2.
"""
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    image=[image1, image2],
    response_format="url",
    watermark=False,
)

Seedream 4.5 API multi-image editing example: left a woman in a white shirt, center a futuristic neon silver bodysuit on a mannequin, right the AI-generated result with the woman wearing the outfit.

这一示例展示了 Seedream 4.5 在保留人脸方面的出色表现;最终图像中的人物仍与原图非常相似。这是其他模型常常表现欠佳的环节。

多图输出编辑

前文介绍的批处理功能同样适用于图像编辑,其中图像总数(输入 + 输出)不能超过 15。这对于基于某个视觉生成多张变体非常有用。

要实现这一点,只需将 image 参数与上文的批量生成参数结合使用。请求示例如下:

prompt = """
Use the given logo to create a visual design system for an outdoor hiking brand, 
including backpack, hat, and shoes.
"""
image = utils.load_image_as_base64("logo.jpeg")
imagesResponse = client.images.generate( 
    model="seedream-4-5-251128", 
    prompt=prompt,
    size="2K",
    image=image,
    sequential_image_generation="auto",
    sequential_image_generation_options=SequentialImageGenerationOptions(max_images=3),
    response_format="url",
    watermark=False
) 

遮罩(Masking)

有些 AI 图像生成模型支持遮罩,允许用户提供黑白遮罩图以指定希望编辑的区域。然而,Seedream 4.5 并不原生支持遮罩参数。

我仍然尝试将遮罩作为输入图像提交,并在提示词中明确需求,看看模型是否能理解并模拟出结果:

Seedream 4.5 API object removal demo: triptych of an anthropomorphic coffee shop scene; center panel shows a white silhouette mask over the bear, and the right panel shows the bear removed via inpainting.

在这个例子中确实成功了,但由于这不是模型的内置功能,因此可靠性不高。比如,当我尝试用同样的方法添加一只狗时,就没有成功。

Seedream 4.5 最佳实践

如上所见,Seedream 4.5 具有良好的性能和一些不错的功能。要充分发挥其价值,请牢记以下最佳实践。

核心提示词指南

为了从 Seedream 4.5 获得最佳的图像生成效果,请在简洁的前提下,同时回答您要什么、为什么要、以及风格应如何:

  • 用朴素语言描述主体、动作与环境。
  • 明确用途/场景(标识、海报、分镜、UI 等)。
  • 用精确关键词或参考图来控制风格。
  • 偏好简洁、准确的提示词,而非冗长的形容词堆叠。
  • 必须出现在图像中的文本请用双引号括起来。

图像编辑指南

编辑图像时,请明确指出需要更改的部分以及必须保持不变的部分。使用以下四个词来指定操作:

  • 添加(Add)
  • 移除(Remove)
  • 替换(Replace)
  • 修改(Modify)

如果场景较复杂,使用箭头、方框、涂鸦等视觉标记来指示区域,有助于模型理解。

当您使用参考图来保持身份、风格或产品特征时,请务必同时说明:

  • 参考目标:要提取/保留的元素(如角色设定、材质、风格)。
  • 生成场景:如何/何处使用(布局、场景、真实度、尺寸、位置)。

批处理指南

使用多张输入图像时,请清晰分配角色:

  • 哪一张提供主体,哪一张提供服装/风格/背景?
  • 以图 1、图 2 等方式引用图像。

尽管模型有参数可指定生成的图像数量,但最好也在提示词中明确我们的需求:

  • 请求生成“一个系列”、“一组”,或直接指定确切的图像数量。
  • 适用于分镜、漫画、一致性的品牌素材集、表情包等。

结语

Seedream 4.5 作为一款成熟、可用于生产的图像生成器,完全能与 Nano Banana 等顶级模型比肩,在保真度、指令遵循与跨图一致性方面表现相当,同时具备成本优势。

令我印象深刻的是其 API 的易用性:同一个 generate 端点即可处理单次生成、编辑(URL 或 base64)、多图融合与序列批量,只需切换少量命名清晰的参数即可启用各模式。

话虽如此,我并不觉得它带来了特别的创新:这是对如今已相当成熟的图像生成工具箱一次出色且成熟的实现,而非概念上的飞跃。

延伸学习

如果您想在本教程的基础上继续提升,不妨夯实让 Seedream 4.5 在生产中顺畅落地的基础能力。

先从 Introduction to APIs in Python 课程入手,掌握认证请求与错误处理;再结合 Image Processing in Python,在流水线中处理尺寸调整、合成与格式转换。

如果您希望更系统地迈向应用型机器学习与工程工具链,AI Engineer 职业路径将这些内容融会贯通,助您构建可靠、可扩展的 AI 功能工作流。

Seedream 4.5 常见问题

Seedream 4.5 与 Nano Banana 相比如何?

虽然在成本与图像质量方面,Seedream 4.5 可与 Nano Banana 的初代版本一较高下,但新版本 Nano Banana 2 使得谷歌在与 Seedream 4.5 的对比中占据优势。

尽管如此,Seedream 4.5 在高保真批量一致性与 4K 写实输出方面表现出色;Nano Banana 则在快速空间推理与精确局部编辑方面更为亮眼。

Seedream 4.5 能生成高分辨率图像吗?

可以。Seedream 4.5 能生成 4K 图像。

生成一张图像需要多少钱?

通过 BytePlus ArkRuntime API 生成 2K 或 4K 图像的费用均为 $0.04 每张。

使用 Seedream 4.5 需要订阅吗?

不需要。我们可以通过 BytePlus ArkRuntime API 使用 Seedream 4.5,仅为生成的图像付费。

可以生成不带水印的图像吗?

可以,将 watermark 参数设置为 False 即可。

François Aubry

作者

François Aubry

全栈工程师,CheapGPT 创始人。我一直热爱教学。从学生时代起,我就积极寻找机会为同学提供辅导与帮助。这份热情促使我攻读博士学位,并在此期间担任助教,以支持我的学术追求。那些年里,我在传统课堂环境中收获了极大的成就感,既建立联系也促进学习。随着在线学习平台的兴起,我也看到了数字教育的变革潜力。事实上,我曾参与我们大学一款此类平台的建设。我致力于将传统教学理念与创新的数字方法相融合。我的热情在于打造既有吸引力、信息量充足,又能让当代学习者轻松获取的课程。

主题