FLUX 3 边界框指南
对照 BFL 文档核对于 2026-10-19;本页新增于 2026-10-19

FLUX 3 Image 边界框 API:完整 Python 示例

一个能直接跑的 FLUX 3 Image 边界框 Python 客户端:拼好描述和 JSON 元素表,带 x-key POST 到 https://api.bfl.ai/v1/flux-3-image,轮询 polling_url 直到 Ready,再保存 result.sample。第二段代码用 src_bbox / tgt_bbox 移动已有图片里的一个元素。

运行前需要

本站是独立指南,不是 Black Forest Labs。请求格式和价格表见主指南,像素换算见坐标换算页。

完整 Python 示例:带框生成布局

import json
import os
import time

import requests

API = "https://api.bfl.ai/v1/flux-3-image"
KEY = os.environ["BFL_API_KEY"]
WAITING = {"Pending", "Reasoning", "Generating"}


def pixels_to_bbox(x, y, w, h, W, H):
    """像素矩形(左上角 x, y,宽 w,高 h)-> 0-1000 的 [top, left, bottom, right]"""
    clamp = lambda v: max(0, min(1000, round(v)))
    return [clamp(y / H * 1000), clamp(x / W * 1000),
            clamp((y + h) / H * 1000), clamp((x + w) / W * 1000)]


def run(payload, out_path, timeout_s=300):
    r = requests.post(API, headers={"x-key": KEY}, json=payload, timeout=60)
    r.raise_for_status()
    task = r.json()                      # id, polling_url, cost(点数)
    print("submitted", task.get("id"), "cost", task.get("cost"))
    deadline = time.time() + timeout_s
    while True:
        time.sleep(1.5)
        res = requests.get(task["polling_url"], headers={"x-key": KEY}, timeout=60).json()
        if res["status"] not in WAITING:
            break
        if time.time() > deadline:
            raise TimeoutError(task["id"])
    if res["status"] != "Ready":         # Error / Request Moderated / Content Moderated / Task not found
        raise RuntimeError(res)
    img = requests.get(res["result"]["sample"], timeout=120)   # 一小时内下载
    img.raise_for_status()
    with open(out_path, "wb") as f:
        f.write(img.content)
    return out_path


caption = ("A square product card: a red ceramic mug <mug_1> on a pale oak table <table_1>, "
           "with a short cream headline <title_1> across the top.")
rows = [
    {"id": "table_1", "bbox": [600, 0, 1000, 1000], "desc": "Pale oak tabletop, soft daylight."},
    {"id": "mug_1", "bbox": [300, 330, 820, 670], "desc": "A red ceramic mug, handle on the right, soft shadow."},
    {"id": "title_1", "bbox": [60, 120, 180, 880], "desc": 'Text reading "Morning List" in a cream serif, one line.'},
]

print(run({"prompt": caption + " " + json.dumps(rows), "aspect_ratio": "1:1", "grounding": False}, "card.png"))

每一行的作用:描述里用尖括号 id 点名每个元素;每行 JSON 用同一个 id 给出 bbox 和 desc;描述和数组之间空一格拼成 prompt。aspect_ratio 要和你画框时的比例一致,因为 0–1000 网格会随画幅拉伸。API 上 grounding 默认开启(可能先联网搜索),示例里关掉,只让你的提示词决定布局。

移动已有图片里的一个元素

编辑时把原图放进 images(第一张是 ref_image_0),行里用 from、src_bbox、tgt_bbox 代替 bbox。保持不动的行两个框相同;要移动的行给新的 tgt_bbox。沿用上面的 run():

def keep(eid, box, desc):
    return {"id": eid, "from": "ref_image_0", "src_bbox": box, "tgt_bbox": box, "desc": desc}

instruction = ("In <ref_image_0>, move the red mug <mug_1> to the left third of the table <table_1>. "
               "Keep the rest of the image unchanged, including the headline <title_1>.")
rows = [
    {"id": "mug_1", "from": "ref_image_0", "src_bbox": [300, 330, 820, 670],
     "tgt_bbox": [300, 60, 820, 400], "desc": "A red ceramic mug, handle on the right."},
    keep("table_1", [600, 0, 1000, 1000], "Pale oak tabletop, soft daylight."),
    keep("title_1", [60, 120, 180, 880], 'Text reading "Morning List" in a cream serif.'),
]
payload = {"prompt": instruction + " " + json.dumps(rows),
           "images": ["https://example.com/card.png"]}   # 换成你自己托管的原图或 base64
print(run(payload, "card_moved.png"))

删除元素:把它的 tgt_bbox 设为 null(Python 里是 None),并在指令里写明。替换或改色:用 "from": None, "src_bbox": None,在 desc 里描述结果。指南建议每个改动在指令和表格里都写一遍。

实际会遇到的错误

限制

框决定位置和大小,不是裁剪蒙版,元素可能略微超出框。编辑时框外像素通常不变,但不保证逐位一致;需要的话生成后再合成。每次调用按 BFL 价格页标价扣点(提交响应里有 cost)。本页代码是为本指南编写的示例,2026-10-19 对照文档请求格式核对过;正式使用前请用你自己的 key 跑一遍。